简体   繁体   中英

How to Get Site ID and site status in IIS 7 using Web Administration C#

I need help in getting site ID and Status on IIS7 using Web Administration. I have this code

 private string getSiteIdByName(string siteName)
    {
        DirectoryEntry root = getDirectoryEntry("IIS://" + textServer.Text + "/W3SVC");
        foreach (DirectoryEntry e in root.Children)
        {
            if (e.SchemaClassName == "IIsWebServer")
            {
                if (e.Properties["ServerComment"].Value.ToString().Equals(siteName, StringComparison.OrdinalIgnoreCase))
                {
                    return e.Name;
                }
            }
        }
        return null;
    }



  private void showStatus(string siteId)
        {
            string result = "unknown";
            DirectoryEntry root = getDirectoryEntry("IIS://" + textServer.Text + "/W3SVC/" + siteId);
            PropertyValueCollection pvc;
            pvc = root.Properties["ServerState"];
            if (pvc.Value != null)
                result = (pvc.Value.Equals((int)ObjectState.Start) ? "Running" :
                          pvc.Value.Equals((int)ObjectState.Stop) ? "Stopped" :
                          pvc.Value.Equals((int)ObjectState.Pause) ? "Paused" :
                          pvc.Value.ToString());
            labelStatus.Text = result + " (" + pvc.Value + ")";
        }

but it really doesn't work well with some of our servers. Is there a way to use Web Administration for this? Any assistance is greatly appreciated.

Thanks!

With a little research, you will find that the WebAdministration API you are talking about is nothing more than a glorified manager for an XML file that IIS goes to great lengths to hide from you. Further, the API tends to perform horribly if you're "reflecting" over multiple web servers; it's using DCOM under the hood, so there's a lot of overhead there that you may not actually be interested in.

When presented with this type of scenario in the past, I learned to bypass Web Administration, and just crack the XML file open with LINQ to XML. The file structure isn't really that complicated once you start looking at it.

It's a standard .config file, located at C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config .

If you want to reach it across servers, use a URI:

file://myserver/c$/windows/system32/inetsrv/config/applicationHost.config

Note, this may be problematic depending on whether or not the account your application is running under has access to the share you're using.

Then, just parse it using LINQ to XML or load it up using the classes in the Configuration namespace.

This configuration file, by the way, contains the Site ID, virtual directory path, physical path, application pool information, and everything you could possibly want to know about every site on the server.

Good luck.

I would definitely not recommend parsing XML manually since there are lots of complexities on getting it right as others suggested, such as encryption, inheritance of default values, and many others that you are likely going to get wrong. It also would mean you cannot access the runtime state, doing it is actually pretty simple you can do:

using(ServerManager serverManager = new ServerManager()) {
    var state = serverManager.Sites[siteName].State;
}

See: enter link description here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM