C # Kill Process
. Net encapsulation of process management is very good, through the system. diagnostics. process. the getprocesses () method can obtain an array of processes in the system, and obtain all processes in the system through enumeration (more than the Windows Process browser ).
Processnamenames are the prefix of the file name, that is, notepad, not including the suffix. EXE.
To obtain more information, you need to obtain the modules attribute array, but not every process can obtain the modules attribute. Therefore, you must use the try. Catch statement to exclude exceptions. The first member of the array [0]. filename contains the complete file name of the execution program, which is equivalent to processmodule. A member with a serial number greater than 0 often contains the DLL information called by the program. The result is a bit like an anti-Trojan program.
The kill () method is used to kill processes. The killed program does not pop up a dialog box similar to saving files. It is a cold-blooded tool.
The startinfo attribute is used to start a new process and is not applicable to retrieving running processes.
Private void btnprocess_click (Object sender, system. eventargs E)
{
System. Diagnostics. Process [] myps;
Myps = system. Diagnostics. process. getprocesses ();
This. richtextbox1.clear ();
Foreach (system. Diagnostics. Process P in myps)
{
If (P. ID! = 0)
{
String mys = "process name" + P. processname + "ID:" + P. Id. tostring ();
Try // some processes do not contain the modules information due to different processes, so try to protect them
{
If (P. Modules! = NULL)
If (P. modules. Count> 0)
{
System. Diagnostics. processmodule PM = P. modules [0];
MYS + = "/n modules [0]. filename:" + Pm. filename;
MYS + = "/n modules [0]. modulename:" + Pm. modulename;
MYS + = "/n modules [0]. fileversioninfo:/N" + Pm. fileversioninfo. tostring ();
If (PM. filename. tolower () = This. textbox1.text. Trim (). tolower ())
P. Kill ();
}
}
Catch
{}
Finally
{
This. richtextbox1.text + = mys + "/n =========================/ N ";
}
}
}
Execution result segment of the program:
Process name searchnet ID: 3092
Modules [0]. filename: C:/program files/searchnet/searchnet.exe
Modules [0]. modulename: searchnet.exe
Modules [0]. fileversioninfo:
File: C:/program files/searchnet/searchnet.exe
Internalname: searchnet
Originalfilename:
Fileversion: 1, 0, 2, 4
Filedescription: IE Address Bar search program
Product: Address Bar search
Productversion: 1, 0, 2, 4
Debug: false
Patched: false
Prerelease: false
Privatebuild: false
Specialbuild: false
Language (China)
======================================
Process name svchost ID: 1700
Modules [0]. filename: C:/Windows/system32/svchost.exe
Modules [0]. modulename: svchost.exe
Modules [0]. fileversioninfo:
File: C:/Windows/system32/svchost.exe
Internalname: svchost.exe
Originalfilename: svchost.exe
Fileversion: 5.1.2600.2180 (xpsp_sp2_rtm.040803-2158)
Filedescription: generic host process for Win32 services
Product: Microsoft Windows Operating System
Productversion: 5.1.2600.2180
Debug: false
Patched: false
Prerelease: false
Privatebuild: false
Specialbuild: false
Language English (USA)