Reading Web Sites Status on IIS with .Net
This is some ability I needed for one of my tools.
Using DirectoryEntry to get information about IIS Web Sites and AppPools.
Code Simple:
public WebSiteEntry[] GetWebSites()
{
string Path = "IIS://" + this.DomainName + "/W3SVC";
DirectoryEntry root = null;
try
{
root = new DirectoryEntry(Path);
}
catch
{
MessageBox.Show("Couldn't access root node", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
return null;
}
if (root == null)
{
MessageBox.Show("Couldn't access root node", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
return null;
}
ArrayList WebSiteList = new ArrayList();
foreach (DirectoryEntry Entry in root.Children)
{
PropertyCollection Properties = Entry.Properties;
try
{
WebSiteEntry Site = new WebSiteEntry();
Site.SiteName = (string)Properties["ServerComment"].Value;
Site.State = (string)Properties["ServerState"].Value.ToString();
<!-- 2 – Start , 4 – Stoped, 6 – Paused -->
WebSiteList.Add(Site);
}
catch { ; }
}
root.Close();
return (WebSiteEntry[])WebSiteList.ToArray(typeof(WebSiteEntry));
}
public class WebSiteEntry
{
public string SiteName = "";
public string State = "";
}