C# 如何通过WMI使网站在当前应用程序池中运行?
我试图找出哪些网站正在运行应用程序池C# 如何通过WMI使网站在当前应用程序池中运行?,c#,iis,wmi,web-administration,C#,Iis,Wmi,Web Administration,我试图找出哪些网站正在运行应用程序池MicrosoftIISv2/WebServerSetting仅提供AppPoolId属性,所有值均为DefaultAppPool。我可以看到所有这些应用程序池都在IIS上运行不同的网站。如何通过WMI在应用程序池上运行网站?我发现可以使用EnumAppsInPool方法来完成。代码如下: public static IEnumerable<string> GetWebSitesRunningOnApplicationPool(Management
MicrosoftIISv2/WebServerSetting
仅提供AppPoolId属性,所有值均为DefaultAppPool。我可以看到所有这些应用程序池都在IIS上运行不同的网站。如何通过WMI在应用程序池上运行网站?我发现可以使用EnumAppsInPool
方法来完成。代码如下:
public static IEnumerable<string> GetWebSitesRunningOnApplicationPool(ManagementScope scope, string applicationPoolName)
{
//get application names from application pool
string path = string.Format("IIsApplicationPool.Name='W3SVC/APPPOOLS/{0}'", applicationPoolName);
ManagementPath managementPath = new ManagementPath(path);
ManagementObject classInstance = new ManagementObject(scope, managementPath, null);
ManagementBaseObject outParams = classInstance.InvokeMethod("EnumAppsInPool", null, null);
//get web server names from application names
IEnumerable<string> nameList = (outParams.Properties["Applications"].Value as string[]) //no null reference exception even there is no application running
.Where(item => !String.IsNullOrEmpty(item)) //but you get empty strings so they are filtered
.ToList() //you get something like /LM/W3SVC/1/ROOT
.Select(item => item.Slice(item.NthIndexOf("/", 2) + 1, item.NthIndexOf("/", 4))); //your WebServer.Name is between 2nd and 4th slahes
//get server comments from names
List<string> serverCommentList = new List<string>();
foreach (string name in nameList)
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, new ObjectQuery(string.Format("SELECT ServerComment FROM IIsWebServerSetting WHERE Name = '{0}'", name)));
serverCommentList.AddRange(from ManagementObject queryObj in searcher.Get() select queryObj["ServerComment"].ToString());
}
return serverCommentList;
}
public static IEnumerable GetWebSitesRunning应用程序池(ManagementScope作用域,字符串applicationPoolName)
{
//从应用程序池获取应用程序名称
string path=string.Format(“IIsApplicationPool.Name='W3SVC/APPPOOLS/{0}'”,applicationPoolName);
ManagementPath ManagementPath=新的管理路径(路径);
ManagementObject classInstance=新的ManagementObject(范围、管理路径、空);
ManagementBaseObject outParams=classInstance.InvokeMethod(“EnumAppsInPool”,null,null);
//从应用程序名称获取web服务器名称
IEnumerable nameList=(outParams.Properties[“Applications”]。值为字符串[])//即使没有应用程序运行,也没有空引用异常
.Where(item=>!String.IsNullOrEmpty(item))//但是您得到的是空字符串,所以它们会被过滤
.ToList()//您会得到类似于/LM/W3SVC/1/ROOT的内容
.Select(item=>item.Slice(item.NthIndexOf(“/”,2)+1,item.NthIndexOf(“/”,4));//您的Web服务器。名称介于第二个和第四个sleas之间
//从名称中获取服务器注释
List serverCommentList=新列表();
foreach(名称列表中的字符串名称)
{
ManagementObjectSearcher搜索器=
新的ManagementObjectSearcher(作用域,新的ObjectQuery(string.Format(“从IIsWebServerSetting中选择ServerComment,其中Name='{0}',Name));
serverCommentList.AddRange(从searcher.Get()中的ManagementObject queryObj选择queryObj[“ServerComment”].ToString());
}
返回服务器评论列表;
}
以及从和中获取的字符串扩展名
public static int NthIndexOf(此字符串目标,字符串值,int n)
{
int结果=-1;
Match m=Regex.Match(目标,(“+value+”).*?{“+n+”});
如果(m.成功)
{
结果=m.Groups[2]。捕获[n-1]。索引;
}
返回结果;
}
公共静态字符串片段(此字符串源、int开始、int结束)
{
如果(结束<0)
{
结束=源。长度+结束;
}
int len=结束-开始;
返回source.Substring(start,len);
}
public static int NthIndexOf(this string target, string value, int n)
{
int result = -1;
Match m = Regex.Match(target, "((" + value + ").*?){" + n + "}");
if (m.Success)
{
result = m.Groups[2].Captures[n - 1].Index;
}
return result;
}
public static string Slice(this string source, int start, int end)
{
if (end < 0)
{
end = source.Length + end;
}
int len = end - start;
return source.Substring(start, len);
}