C # How to start/kill/find a process

Source: Internet
Author: User

It is easy to search for/sort, but the system command ntsd.exe can be used as a result. For detailed usage, see the following code:

Using system;
Using system. diagnostics;
Using system. Windows. forms;

Namespace processdemo
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

Private void form1_load (Object sender, eventargs E)
{
This. linklabel1.links. Add (0, linklabel1.text. length, "http://yjmyzz.cnblogs.com /");
}

Private void linklabel1_linkclicked (Object sender, linklabellinkclickedeventargs E)
{
This. linklabel1.links [linklabel1.links. indexof (E. Link)]. Visited = true;
String target = E. Link. linkdata as string;
If (target! = NULL & target. startswith ("http ://"))
{
Process. Start (target );
}
}

/// <Summary>
/// List all accessible processes
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void btnlist_click (Object sender, eventargs E)
{
Process [] processes;
Processes = process. getprocesses ();
String STR = "";
Foreach (PROCESS p in processes)
{
Try
{
STR = P. processname;
This. lst1.items. add ("name:" + P. processname + ", start time:" + P. starttime. toshorttimestring () + ", process ID:" + P. id. tostring ());
}
Catch (exception ex)
{
This. lst1.items. Add (ex. Message. tostring (); // some system processes prohibit access, so add Exception Processing.
}
}

}

Private void btnfind_click (Object sender, eventargs E)
{
Txtfind. Text = txtfind. Text. Trim (). tolower ();
If (txtfind. Text. length> 0)
{
Process [] ARRP = process. getprocesses ();
Foreach (PROCESS p in ARRP)
{
Try
{
If (P. processname. tolower () = txtfind. Text)
{
MessageBox. Show (txtfind. Text + "found, PID is" + P. Id. tostring ());
Return;
}
}
Catch {}
}

MessageBox. Show ("this process is not found. Please check the input! ");
}
}

Private void btnkill_click (Object sender, eventargs E)
{
Txtfind. Text = txtfind. Text. Trim (). tolower ();
Int pid =-1;
If (txtfind. Text. length> 0)
{
Process [] ARRP = process. getprocesses ();
Foreach (PROCESS p in ARRP)
{
Try
{
If (P. processname. tolower () = txtfind. Text)
{
PID = P. ID;
Break;
}
}
Catch {}
}

If (PID! =-1)
{
Runcmd ("ntsd-C q-P" + PID );
}

}
}

/// <Summary>
/// Run the doscommand
/// Dos process shutdown command (ntsd-C q-p pid) PID is the process ID
/// </Summary>
/// <Param name = "command"> </param>
/// <Returns> </returns>
Public String runcmd (string command)
{

PROCESS p = new process ();

P. startinfo. filename = "cmd.exe ";
P. startinfo. Arguments = "/C" + command;
P. startinfo. useshellexecute = false;
P. startinfo. redirectstandardinput = true;
P. startinfo. redirectstandardoutput = true;
P. startinfo. redirectstandarderror = true;
P. startinfo. createnowindow = true;

P. Start ();

Return P. standardoutput. readtoend ();

}

}
}

In addition, ntsd.exe is available for Versions later than Windows Vista (including Windows Vista download ).

Http://files.cnblogs.com/yjmyzz/ntsd.rar

 

 

 

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.