How to view the w3wp process by C #?

Source: Internet
Author: 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 " ; // Set callProgramName
P. startinfo. useshellexecute =   False ; // Disable shell usage
P. startinfo. redirectstandardinput =   True ; // Redirect standard input
P. startinfo. redirectstandardoutput =   True ; // Redirection standard output
P. startinfo. redirectstandarderror =   True ; // Redirect error output
P. startinfo. createnowindow =   True ; // Do not display the Configuration window

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) // whether a website is started
{
// Regular Expression Extraction
// For example: WP "29308" <applicationpool: profile3.csdn.net>
String Pattern =   " WP \ " \ D + \ " . + " ;
List < String > List =   New List < String > ();
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 useProcessObject To run the cmd.exe process, and then run the "appcmd list WP" command. after it, We can receive the output string, and analyze the string to get the result of what we want to get.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.