Freigeben über


IIS7 – Search the complete IIS configuration store – in which web.config is your attribute configured

As you know IIS 7.0 configuration system is huge, broad, or whatever you feel like calling it. It no longer just exist in its main configuration file applicationHost.config (like Metabase.XML in IIS 6.0). It can be widely spread in any of the web.config files in the virtual directories, or applications.

Today I had an interesting chat with my colleague on finding a tool to list down what are the web.config files available in a given website. I was suggesting the below code using MWA to just parse the configuration, and print if any VirtualPath of an application (virtual directory) has a file named web.config.

Update: The below sample doesn't list down the web.config's which are there in any physical folder which is not configured as an Application. So, you need to tweak your code a bit. Thanks to Vijay pointing this out.

Sample code snippet in C# listing down all applications, virtual directories available

 ServerManager sm = new ServerManager();
 foreach (Site s in sm.Sites)
 {
     Console.WriteLine(s.Name + "\n");
     foreach (Application a in s.Applications)
     {
         foreach (VirtualDirectory v in a.VirtualDirectories)
         {
             try
             {
                 string[] files = Directory.GetFiles(v.PhysicalPath);
                 for (int i = 0; i < files.Length; i++)
                     if (files[i].EndsWith("web.config"))
                         Console.WriteLine(a + " has " + files[i]);
             }
             catch (Exception e)
             {
                 // invalid physical path, so ignore :) 
             }
         }
     }
     Console.WriteLine("\n");
 }

But, I wasn’t really thinking that there is a powerful module “Configuration Editor” which gives you an easy way to search for attributes, and know where they are configured (physical path of the web.config file). Below is a sample screenshot which shows the use of Search feature.

 

image

So, next time when you want to know where all the given attribute is configured, consider using Configuration Editor. Hope this helps!

Comments