首页 | 麦客学吧 | 视频教程 | FLASH小游戏 | 素材下载 | 常用工具
您当前的位置:首页 > 程序开发 > C# > 正文

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

出处:麦客学吧 [2007-6-28 17:26:39] 作者:佚名 点击数:168

讨论交流:http://x8.maicoo.com/team/8668.html

自从用了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
{
    /**//// <summary>
    /// W3wp 的摘要说明。
    /// </summary>
    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简直一模一样。
http://www.cnblogs.com/karoc/archive/2006/11/24/571245.html

【责任编辑: lanier

素材中心
麦客酷站赏析频道

精彩图文推荐

关于本站 - 联系站长 - 广告服务 - 合作伙伴 - 网站地图 - 版权声明 - 报告错误 - 收藏本站 | Http://www.MaiCoo.com

Copyright © 2005 - 2008 MaiCoo.com All Rights Reserved

违法和不良信息举报中心 本站服务器空间和带宽由雷克斯网络提供赞助
浙ICP备06017818号