title: How do I determine the ASP.NET version of a virtual directory or website using C#?
body: <p>How do I find out the .NET framework of an IIS virtual directory is using in C#.  I need to display it to the user.</p>
  
  <pre><code>using System.DirectoryServices;
  using System.IO;
  
  private enum IISVersion
  {
      IIS5,
      IIS6
  }
  
  private void ReadVirtualDirectory(string server, string directory, IISVersion version)
  {
      string siteID = string.Empty;
      string path = string.Empty;
  
      switch(verison)
      {
          case IISVersion.IIS5:
              path = string.Format("IIS://{0}/W3SVC/1/Root", server);
  
              if(!string.IsNullOrEmpty(directory))
              {
                  path = string.Concat(path, "/", directory);
              }
  
              break;
  
          case IISVersion.IIS6:
              path = string.Format("IIS://{0}/W3SVC", server);
  
              if(!string.IsNullOrEmpty(directory))
              {
                  DirectoryEntry de = new DirectoryEntry(path);
  
                  foreach(DirectoryEntry child in de.Children)
                  {
                      if(child.Properties["ServerComment"].Value.ToString().ToLower() == directory)
                      {
                          siteID = child.Name;
                          break;
                      }
                  }
  
                  path = string.Concat(path, "/", siteID);
  
                  de.Close()
                  de = null;
              }
  
              break;
      }
  
      DirectoryEntry iis = new DirectoryEntry(path);
  
      //display iis properties here...
      //need to display if the virtual directory is running under .NET 1.1 or 2.0
  
      iis.Close();
      iis = null;       
  }
  </code></pre>