title: Trying to read web.config file for an IIS site from a Windows service
body: <p>I am trying to look for a special <code>web.config</code> file for a web site installed on a local IIS. I do this search from a Windows service. I do the following:</p>
  
  <pre><code>using (ServerManager serverManager = new ServerManager())
  {
      for (int r = 0; r &lt; serverManager.Sites.Count; r++)
      {
          string strSiteName = serverManager.Sites[r].Name;
          ApplicationCollection arrApps = serverManager.Sites[r].Applications;
  
          for (int a = 0; a &lt; arrApps.Count; a++)
          {
              Microsoft.Web.Administration.Application aa = arrApps[a];
              foreach (VirtualDirectory vd2 in aa.VirtualDirectories)
              {
                  string strPhysPath = Environment.ExpandEnvironmentVariables(vd2.PhysicalPath);
                  int rr = 0;
  
                  try
                  {
                      Configuration cnfg = serverManager.GetWebConfiguration(strSiteName, strPhysPath);
                      if (cnfg != null)
                      {
                          string swww = getWebConfigGeneralParamValue(cnfg, "SpecNodeName");
                      }
                  }
                  catch
                  {
                      //Error
                  }
  
              }
          }
  
      }
  </code></pre>
  
  <p>Where,</p>
  
  <pre><code>public static string getWebConfigGeneralParamValue(Configuration config, string strParamKey)
  {
      string strRes = null;
  
      try
      {
          ConfigurationSection configSection1 = config.GetSection("configuration");
          if (configSection1 != null)
          {
              ConfigurationElement configGeneralParams = configSection1.GetChildElement("GeneralParams");
              if (configGeneralParams != null)
              {
                  ConfigurationElementCollection configCol = configSection1.GetCollection();
                  if (configCol != null)
                  {
                      strRes = configCol[strParamKey].ToString();
                  }
              }
          }
      }
      catch(Exception ex)
      {
          strRes = null;
      }
  
      return strRes;
  }
  </code></pre>
  
  <p>and the web.config file that should be recognized by this script is something like this:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!--
    For more information on how to configure your ASP.NET application, please visit
    http://go.microsoft.com/fwlink/?LinkId=169433
    --&gt;
  &lt;configuration&gt;
   &lt;!-- regular ASP.NET config stuff --&gt;
   &lt;GeneralParams&gt;
    &lt;param key="SpecNodeName" value="Special Node Value"/&gt;
   &lt;/GeneralParams&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>But what I get is that <code>config.GetSection("configuration");</code> throws this exception:</p>
  
  <blockquote>
    <p>{"Filename:
    \\?\C:\inetpub\wwwroot\C:\Users\Dev\Desktop\CSharp\MyTestWebApp\MyTestWebApp\web.config\r\nError:
    The configuration section 'configuration' cannot be read because it is
    missing a section declaration\r\n\r\n"}</p>
  </blockquote>
  
  <p>Any idea how to make it work?</p>