locked
IIS website size using DirectoryEntry RRS feed

  • Question

  • Hello,

    I am using the following code to loop through my websites in IIS:

    DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
    foreach (DirectoryEntry directory in root.Children)
    {
        if (directory.SchemaClassName.Equals("IIsWebServer"))
        {
            // ...
        }
    }

    What I would like to do is use the available information to get the exact size of the directory of each website. Is this possible, and if so, how?

    Thanks!

    Monday, May 4, 2009 12:51 PM

Answers

  • Hi,

    The code works here:

    //_portNumber is the port number of the website
    //_name is the name of the virtual directory
            public string GetVirtualDirectory(string _portNumber, string _name)
            {
                
                string identifier = null;

                DirectoryEntry root = new DirectoryEntry("IIS://LOCALHOST/W3SVC");
                foreach (DirectoryEntry e in root.Children)
                {
                    if (e.SchemaClassName == "IIsWebServer")
                    {
                        foreach (object property in e.Properties["ServerBindings"])
                        {
                            if (property.Equals(":" + _portNumber + ":"))
                            {
                                identifier = e.Name;
           break;
                            }
                        }

          if(identifier != null)
          {
              break;
          }
                    }
                }

         if(identifier != null)
         {
             identifier = "1";
         }

                DirectoryEntry de = new DirectoryEntry("IIS://LOCALHOST/W3SVC/" + identifier + "/ROOT/" + _name);
                string path = (string)de.Properties["Path"].Value;
                return path;
            }

    The code is excerpted from a chinese website:
    http://www.diybl.com/course/4_webprogram/asp.net/netjs/2008530/118231.html


    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Harry Zhu Tuesday, May 12, 2009 7:33 AM
    • Marked as answer by Harry Zhu Thursday, May 14, 2009 7:21 AM
    Friday, May 8, 2009 8:06 AM

All replies

  • public static void EnumerateWebsites()
       {
          try
          {
             DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");
             
             foreach(DirectoryEntry de in w3svc.Children)
             {
                if(de.SchemaClassName == "IIsWebServer")
                {
                    Console.WriteLine(de.Name);
                }         
             }
          }
          catch(Exception ex)
          {
             Console.WriteLine("Error in EnumerateWebsites: " + ex.Message);
          }
       }
    static string GetIISSiteName(string iisHost, string serverComment)
        {
            string adsiPath = iisHost + "/W3SVC";
            DirectoryEntry entry = new DirectoryEntry(adsiPath);
            foreach (DirectoryEntry site in entry.Children)
            {
                if (site.SchemaClassName == "IIsWebServer" &&
                    site.Properties["ServerComment"].Value
                    .ToString().Equals(serverComment))
                {
                    return site.Name;
                }
            }
    
            return "";
        }
    
    
        static string GetVirtualDirPath(string iisHost,
                                        string siteName, string vdName)
        {
            string adsiPath = iisHost + "/W3SVC/" + siteName + "/Root/" + vdName;
    
            try
            {
                DirectoryEntry entry = new DirectoryEntry(adsiPath);
                return entry.Properties["Path"].Value.ToString();
            }
            catch(Exception ex)
            {
                // If Virtual Directory is not found,
                // it will throw exception.
                return "";
            }
    
            return "";
        }
    public static long DirSize(DirectoryInfo d) 
        {    
            long Size = 0;    
            // Add file sizes.
            FileInfo[] fis = d.GetFiles();
            foreach (FileInfo fi in fis) 
            {      
                Size += fi.Length;    
            }
            // Add subdirectory sizes.
            DirectoryInfo[] dis = d.GetDirectories();
            foreach (DirectoryInfo di in dis) 
            {
                Size += DirSize(di);   
            }
            return(Size);  
        }
    


    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    Monday, May 4, 2009 1:06 PM
  • Thanks for your reply, however, you aren't showing how you call the 'DirSize' method. How do I get DirectoryInfo from a DirectoryEntry? Edit: Also, DirectoryEntry does not seem to have the property Path, although you are using it in your example.
    Monday, May 4, 2009 1:22 PM
  • 1. Get the list of websites
    2. Get virtual path
    2. Get the physical path of website
    3. Get the Directory size


    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    Monday, May 4, 2009 1:32 PM
  • 1. Get the list of websites
    2. Get virtual path
    2. Get the physical path of website

    3. Get the Directory size

    These two steps are exactly the problem and you do not seem to have provided the code to do this. I can get the Virtual Path, but need the Physical Path.

    Monday, May 4, 2009 2:00 PM
  • Perhaps I need to be more clear?

    The problem is that I am unable to get the physical path of a website.

    The only way I found to do this was by using the Server.MapPath():string method, but the application I am working on is a console application and not an asp.net application.

    Thanks
    Wednesday, May 6, 2009 12:00 PM
  • Hi,

    The code works here:

    //_portNumber is the port number of the website
    //_name is the name of the virtual directory
            public string GetVirtualDirectory(string _portNumber, string _name)
            {
                
                string identifier = null;

                DirectoryEntry root = new DirectoryEntry("IIS://LOCALHOST/W3SVC");
                foreach (DirectoryEntry e in root.Children)
                {
                    if (e.SchemaClassName == "IIsWebServer")
                    {
                        foreach (object property in e.Properties["ServerBindings"])
                        {
                            if (property.Equals(":" + _portNumber + ":"))
                            {
                                identifier = e.Name;
           break;
                            }
                        }

          if(identifier != null)
          {
              break;
          }
                    }
                }

         if(identifier != null)
         {
             identifier = "1";
         }

                DirectoryEntry de = new DirectoryEntry("IIS://LOCALHOST/W3SVC/" + identifier + "/ROOT/" + _name);
                string path = (string)de.Properties["Path"].Value;
                return path;
            }

    The code is excerpted from a chinese website:
    http://www.diybl.com/course/4_webprogram/asp.net/netjs/2008530/118231.html


    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Harry Zhu Tuesday, May 12, 2009 7:33 AM
    • Marked as answer by Harry Zhu Thursday, May 14, 2009 7:21 AM
    Friday, May 8, 2009 8:06 AM
  • Thanks for the reply again, but this method is not working.

    Also, the method name clearly says GetVirtualDirectory while I need the Physical path.

    As stated above, I already have the code needed to iterate through my websites:

                foreach (DirectoryEntry directory in root.Children)
                {
                    if (directory.SchemaClassName.Equals("IIsWebServer"))
                    {





    }
    }
    Saturday, May 9, 2009 11:33 AM
  • Hi,

    Well, the comment to the method is :"getting the local path of the virtual path", maybe the method is not named properly.

    I tested the code here , and it works .
     Please have a try :)

    Harry

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Sunday, May 10, 2009 3:51 AM