title: How to read an IIS 6 Website's Directory Structure using WMI?
body: <p><strong>I need to read a website's folders</strong> using WMI and C# in IIS 6.0. I am able to read the Virtual directories and applications using the "IISWebVirtualDirSetting" class. However the <strong><em>physical folders located inside a website cannot be read using this class.</em></strong> </p>
  
  <p><strong>And for my case i need to read sub folders located within a website</strong> and later on set permission on them. For my requirement i dont need to work on Virtual Directories/Web Service Applications (which can be easily obtained using the code below..).</p>
  
  <p>I have tried to use IISWebDirectory class but it has been useful.</p>
  
  <p>Here is the code that reads IIS Virtual Directories...</p>
  
  <pre><code>public static ArrayList RetrieveVirtualDirList(String ServerName, String WebsiteName)
  {
      ConnectionOptions options = SetUpAuthorization();
      ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISV2", ServerName), options);
      scope.Connect();
      String SiteId = GetSiteIDFromSiteName(ServerName, WebsiteName);
      ObjectQuery OQuery = new ObjectQuery(@"SELECT * FROM IISWebVirtualDirSetting");
      //ObjectQuery OQuery = new ObjectQuery(@"SELECT * FROM IIsSetting");
  
      ManagementObjectSearcher WebSiteFinder = new ManagementObjectSearcher(scope, OQuery);
      ArrayList WebSiteListArray = new ArrayList();
      ManagementObjectCollection WebSitesCollection = WebSiteFinder.Get();
      String WebSiteName = String.Empty;
      foreach (ManagementObject WebSite in WebSitesCollection)
      {
          WebSiteName = WebSite.Properties["Name"].Value.ToString();
          WebsiteName = WebSiteName.Replace("W3SVC/", "");
          String extrctedSiteId = WebsiteName.Substring(0, WebsiteName.IndexOf('/'));
          String temp = WebsiteName.Substring(0, WebsiteName.IndexOf('/') + 1);
          String VirtualDirName = WebsiteName.Substring(temp.Length);
          WebsiteName = WebsiteName.Replace(SiteId, "");
          if (extrctedSiteId.Equals(SiteId))
          //if (true)
          {
              WebSiteListArray.Add(VirtualDirName );
              //WebSiteListArray.Add(WebSiteName);
              //+ "|" + WebSite.Properties["Path"].Value.ToString()
          }
      }
      return WebSiteListArray;
  }
  </code></pre>
  
  <p><strong>P.S:</strong></p>
  
  <p>I need to programmatically get the sub folders of an already deployed site(s) using WMI and C# in an ASP. Net Application. I need to find out the sub folders of existing websites in a local or remote IIS 6.0 Web Server. So i require a programmatic solution. Precisely if i am pointed at the right class (like IISWebVirtualDirSetting etc ) that i may use for retrieving the list of physical folders within a website then it will be quite helpful. </p>
  
  <p>I am not working in Powershell and i don't really need a solution that involves powershell or vbscripts.</p>
  
  <p>Any alternative programmatic way of doing the same in C#/ASP.Net will also be highly appreciated.</p>
  
  <p>EDIT:</p>
  
  <h2>GetSiteIdFromSiteName</h2>
  
  <p>The code may look like this:</p>
  
  <pre><code>public static int GetSiteIdFromSiteName(String ServerName, String WebsiteName)
  {
      ConnectionOptions options = SetUpAuthorization();
      ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISV2", ServerName), options);
      scope.Connect();
      ObjectQuery OQuery = new ObjectQuery(@"SELECT * FROM IIsSetting");
  
      ManagementObjectSearcher WebSiteFinder = new ManagementObjectSearcher(scope, OQuery);
      ArrayList WebSiteListArray = new ArrayList();
      ManagementObjectCollection WebSitesCollection = WebSiteFinder.Get();
      String WebSiteName = String.Empty;
      foreach (ManagementObject WebSite in WebSitesCollection)
      {
          WebSiteName = WebSite.Properties["Name"].Value.ToString();
          WebsiteName = WebSiteName.Replace("W3SVC/", "");
          String extrctedSiteId = WebsiteName.Substring(0, WebsiteName.IndexOf('/'));
          break;  
      }
      return extrctedSiteId;
  }
  </code></pre>
  
  <p>and</p>
  
  <h2>SetupAuthorization()</h2>
  
  <p>Here is the SetupAuthorization function.</p>
  
  <pre><code>    public ConnectionOptions SetUpAuthorization()
  { 
          ConnectionOptions options = new ConnectionOptions();
      //options.Authentication = AuthenticationLevel.Connect; 
      //may need to set Packetprivacy enum
      options.Authentication = AuthenticationLevel.PacketPrivacy; 
      options.EnablePrivileges = true; 
      options.Impersonation = ImpersonationLevel.Impersonate; 
  
       return options;
   } 
  </code></pre>
  
  <p>Regards</p>