Click here to Skip to main content
15,922,015 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Am Trying to get List of Websites hosted on IIS. But am getting Exception

COMException was unhandled. Access is denied.



CODE:

C#
static void Main(string[] args)
       {
           foreach (WebSites site in GetSites("IIS://localhost/W3SVC"))
           {
               Console.WriteLine(String.Concat(site.Name, ",", site.Identity, ",", site.Status, ",", site.PhysicalPath));
           }
       }
       static IEnumerable<WebSites> GetSites(String path)
       {
           DirectoryEntry IIsEntities = new DirectoryEntry(path);
           foreach (DirectoryEntry IIsEntity in IIsEntities.Children)
           {
               if (IIsEntity.SchemaClassName == "llsWebServer")
               {
                   yield return new WebSites
                       (
                       Convert.ToInt32(IIsEntity.Name),
                       IIsEntity.Properties["ServerComment"].Value.ToString(),
                       GetPath(IIsEntity),
                       (ServerState)IIsEntity.Properties["ServerState"].Value
                       );
               }
           }
       }
       static String GetPath(DirectoryEntry IIsWebServer)
       {
           foreach (DirectoryEntry IIsEntity in IIsWebServer.Children)
           {
               if (IIsEntity.SchemaClassName == "IIsWebVirtualDir")
                   return IIsEntity.Properties["path"].Value.ToString();
           }
           return null;

       }

   }



Can any one help me.



Thanks Regards
sam.198979
Posted
Comments
Richard C Bishop 21-May-13 11:23am    
You not not have the credentials to access it apparently.
sam.198979 21-May-13 11:25am    
Then do i need any permissions.
Richard C Bishop 21-May-13 11:29am    
Maybe not, when debugging, where does the application stop at?
sam.198979 21-May-13 11:36am    
foreach (DirectoryEntry IIsEntity in IIsEntities.Children)

Richard C Bishop 21-May-13 11:40am    
I found an example that was claimed to be working. It returns a bool so you will need change that to a string, but the method of getting the sites should be intact. Of course the path will be your own as well.

1 solution

I found this example that was claimed to be working:
try
{
  DirectoryEntry iis = new DirectoryEntry("IIS://localhost/w3svc");
  foreach(DirectoryEntry site in iis.Children)
  {
    if(site.SchemaClassName == "IIsWebServer")
    {
      if(domain.Equals(site.Properties["ServerComment"].Value.ToString()))
      {
        return //your value here;
      }
    }
  }
}
catch(Exception e)
{
      ...
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900