问题
I got a task of creating an Web application in C# ,which fetches IIS and App Pool details of Website hosted in the remote server(same location).Any idea or help is well appreciated!!!
-Renji
回答1:
This is somehow a broad question, but in order to help here are some points you can start from:
- get the website name at IIS:
System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName()
- to get the list of websites and virtual dirs, check this: How to programmatically get sites list and virtual dirs in IIS 7?
- to manage IIS: http://www.codeproject.com/Articles/99634/Use-C-to-manage-IIS
- get IIS version: How to detect IIS version using C#?
- get site status: Programmatically get site status from IIS, gets back COM error
I guess this is enough for you to start exploring everything related to IIS, hope it helps.
回答2:
I was also having the same requirement. On doing so many trail and error approach I got the result.
Pre-Requisites
- IIS 6 and Above
- .Net Framework 4.0 and Above
- Add reference to -System.DirectoryServices and Microsoft.Web.Administration
c# Console Application Method
public static SortedDictionary<string,string> GetApplicationPoolNames ( string mname = null )
{
try
{
ServerManager manager = new ServerManager ();
SortedDictionary<string,string> ApplicationPoolStatus = new SortedDictionary<string,string> ();
if (string.IsNullOrEmpty (mname))
mname = System.Environment.MachineName;
string appPoolName = string.Empty;
manager = ServerManager.OpenRemote (mname);
ApplicationPoolCollection applicationPoolCollection = manager.ApplicationPools;
foreach (ApplicationPool applicationPool in applicationPoolCollection)
{
if (!string.IsNullOrEmpty (applicationPool.Name))
{
if (!ApplicationPoolStatus.ContainsKey (applicationPool.Name))
{
ApplicationPoolStatus.Add (applicationPool.Name,string.Empty);
}
ApplicationPoolStatus[applicationPool.Name] = applicationPool.State.ToString ();
}
}
return ApplicationPoolStatus;
}
catch (Exception)
{
throw;
}
}
ASP.Net Web/MVC Application Method
public SortedDictionary<string,string> ShowApplicationPoolDatas ()
{
SortedDictionary<string,string> ApplicationPoolStatus = new SortedDictionary<string,string> ();
var domain = this.HttpContext.Request.Url.Host;
DirectoryEntry Services = new DirectoryEntry ("IIS://"+ domain + "/W3SVC/APPPOOLS");
foreach (DirectoryEntry Entry in Services.Children)
{
if (!string.IsNullOrEmpty (Entry.Name))
{
if (!ApplicationPoolStatus.ContainsKey (Entry.Name))
{
ApplicationPoolStatus.Add (Entry.Name,string.Empty);
}
}
var intStatus = (Int32)Entry.InvokeGet ("AppPoolState");
switch (intStatus)
{
case 2:
ApplicationPoolStatus[Entry.Name] = "Running";
break;
case 4:
ApplicationPoolStatus[Entry.Name] = "Stopped";
break;
default:
ApplicationPoolStatus[Entry.Name] = "Unknown";
break;
}
}
return ApplicationPoolStatus;
}
来源:https://stackoverflow.com/questions/18349532/an-application-which-fetches-iis-and-app-pool-details-of-website-hosted-in-remot