tags:

views:

207

answers:

2

I need to read the contents of the Web.Config and send them off in an email, is there a better way to do the following:

        string webConfigContents = String.Empty;
        using (FileStream steam = new FileStream(
                 Server.MapPath("~/web.config"),
                 FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            using (StreamReader reader = new StreamReader(steam))
            {
                webConfigContents = reader.ReadToEnd();
            }
        }

I dont want to lock the file. Any ideas?

Edit - I need a raw dump of the file, I cant attach the file (Webhost says nay!), and im not looking for anything specific inside it

A: 

By using the asp.net api.

System.Configuration.Configuration rootWebConfig1 =

System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
if (0 < rootWebConfig1.AppSettings.Settings.Count)
{
  System.Configuration.KeyValueConfigurationElement customSetting = 
  rootWebConfig1.AppSettings.Settings["customsetting1"];
  if (null != customSetting) {
    Console.WriteLine("customsetting1 application string = \"{0}\"",                  customSetting.Value);
  }
  else {
    Console.WriteLine("No customsetting1 application string");
  }
}
Sander Versluys
ok, i now see you need a raw exact copy of the file, this ain't it idd...
Sander Versluys
+3  A: 

You can replace your code with this:

string webConfigContents = File.ReadAllText(Server.MapPath("~/web.config"));

The file will be locked for writing while it is being read, but not for reading. But I don't think this will be a problem, because the web.config file is not usually being written to (since this would restart the web application).

M4N