How to view the W3WP process by c#?

來源:互聯網
上載者:User

   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) //是否有啟動的網站
            {
                //Regex提取
                //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.

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.