zoukankan      html  css  js  c++  java
  • How to view the W3WP process by c#?

    We need to know which w3wp process is running now When we debug my website in the Visual Studio. As we know, we can use this cmd(%windir%/system32/inetsrv/appcmd list wp) to show the result in the windows2008. But, How to view the W3WP process by c#?

    I give the method to you, please look this code:

     static void Main(string[] args)
            {
                string str = CmdListWP();
                Console.WriteLine(str);
                Console.WriteLine("Please press any key to exit!");
                Console.ReadLine();
            }
    
            private static string CmdListWP()
            {
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";                    //设定调用程序的名称
                p.StartInfo.UseShellExecute = false;                  //关闭Shell的使用
                p.StartInfo.RedirectStandardInput = true;         //重定向标准输入
                p.StartInfo.RedirectStandardOutput = true;      //重定向标准输出
                p.StartInfo.RedirectStandardError = true;          //重定向错误输出
                p.StartInfo.CreateNoWindow = true;                //配置不显示窗口
    
                string output;
                p.Start();
                try
                {
                    p.StandardInput.WriteLine("%windir%/system32/inetsrv/appcmd list wp");
                    p.StandardInput.WriteLine("exit");
                    output = p.StandardOutput.ReadToEnd();
                }
                finally
                {
                    p.Close();
                }
    
                string result = string.Empty;
                if (output.IndexOf("WP \"") != -1) //是否有启动的站点
                {
                    //正则表达式提取
                    //for example: WP "29308" <applicationPool:profile3.csdn.net>
                    string pattern = "WP \"\\d+\" .+";
                    List<string> list = new List<string>();
                    Regex regex = new Regex(pattern);
                    if (regex.IsMatch(output))
                    {
                        MatchCollection mc = regex.Matches(output);
                        for (int i = 0; i < mc.Count; i++)
                        {
                            list.Add(mc[i].Value);
                        }
                    }
                    foreach (string str in list)
                    {
                        result += str + "\n";
                    }
                }
                return result;
            }

    Please take attention to the code: Process p = new Process();
    we can use the Process object to run the cmd.exe process, and then run the "appcmd list wp" command. after it, we can receive the output string, and analyse the string to get the result of what we want to get.


    作者:Angelo Lee
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    忘记密码破解
    关于本地变量的理解
    MVC的请求过程(或者MVC三者的关系)
    static 静态 关键字
    博客搬家通知
    C#一个可以马上跑起来的反射例子Assembly的使用
    C#之DateTime日期格式解析
    AddressParsing在C#中好用的地址拆分地址结构化库Net5
    C#中获取本地IP地址方法
    ComdeDom生成对象Emit之引用其他成员类库
  • 原文地址:https://www.cnblogs.com/yefengmeander/p/2887652.html
Copyright © 2011-2022 走看看