w3wp对应进程_用C#+WMI实现获取w3wp进程对应的应用程序池

自从用了ASP.NET2.0以后,这个问题被渐渐关注起来,目前的方法就是调用iisapp.vbs获取。

今天准备在我的文本转换工具里集成这个功能,于是,用C#实现了一下。

using System;

using System.Text;

using System.Text.RegularExpressions;

using System.Diagnostics;

using System.Management;

using System.Windows.Forms;

namespace TextConvertor

{

/**

/// W3wp 的摘要说明。

///

public class W3wp

{

private W3wp(){}

public static string GetAllW3wp(string input)

{

ObjectQuery oQuery = new ObjectQuery("select * from Win32_Process where Name='w3wp.exe'");

ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oQuery);

ManagementObjectCollection oReturnCollection = oSearcher.Get();

string pid;

string cmdLine;

StringBuilder sb = new StringBuilder() ;

foreach(ManagementObject oReturn in oReturnCollection)

{

pid = oReturn.GetPropertyValue("ProcessId").ToString();

cmdLine = (string)oReturn.GetPropertyValue("CommandLine");

string pattern = "-ap \"(.*)\"" ;

Regex regex = new Regex(pattern, RegexOptions.IgnoreCase) ;

Match match = regex.Match(cmdLine) ;

string appPoolName = match.Groups[1].ToString() ;

sb.AppendFormat("W3WP.exe PID: {0} AppPoolId:{1}\r\n", pid, appPoolName );

}

return sb.ToString();

}

}

}

实现的原理和VBScript简直一模一样。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值