This document uses a simple example to describe how to use C # To obtain the current active process and read the basic information of a specific ID process.
Nowadays, many software have the function of reading system processes. The famous one is customizer XP, which is a software used to set various settings of Windows XP. One function of this software is to enable or disable programs loaded during system running. Of course, this article is not as elegant and refined as customizer XP, but as long as I know the basic method, it is very easy to make such a function.
In this example, the process class of. NET Framework is used to obtain information about the activation process. The list of the currently activated processes is provided, and some basic information about a process can be displayed.
Production Process
1. Run Visual Studio. NET to create a Visual C #. Net project. The project name is processinfo.
2. In Solution Explorer, change form1.cs to frmprocessinfo. CS (this option is optional ).
3. Change the text attribute of form1 to process information, font to verdana, and 9pt (optional ).
4. Add three controls to the form: two buttons and a textbox. Modify the control attributes as follows: Control name attribute text attribute
Button1 btngetprocesslist click here to get the list of currently activated Processes
Button 2 btngetprocessbyid obtains the process information of the specified ID:
Textbox txtprocessid N/
Adjust the position of each control. The user interface is similar:
5. Go to the frmprocessinfo. CS code editor. Add the following line of code to the file header:
Using system. diagnostics;
[Note] the system. Diagnostics namespace provides specific classes that allow you to interact with system processes, event logs, and performance counters.
6. Double-click the btngetprocesslist button in frmprocessinfo. CS [design. The system automatically adds the btngetprocesslist_click event. Enter the following code to the btngetprocesslist_click event:
String STR = "";
Process [] processes;
// Get the list of current active processes.
Processes = system. Diagnostics. process. getprocesses ();
// Grab some basic information for each process.
Process process;
For (INT I = 0; I <processes. Length-1; I ++)
{
Process = processes [I];
STR = STR + convert. tostring (process. ID) + ":" +
Process. processname + "/R/N ";
}
// Display the process information to the user
System. Windows. Forms. MessageBox. Show (STR );
// Default the textbox value to the first process ID-for the getbyid button
Txtprocessid. Text = processes [0]. Id. tostring ();
[Note] the process component provides access to processes running on the computer. In the simplest words, a process is the currently running application. A thread is the basic unit for the operating system to allocate processor time to it. Code of any part of a thread executable process, including the part currently executed by another thread.
The process component is a useful tool for starting, stopping, controlling, and monitoring applications. You can use the process component to obtain the list of processes currently running or start a new process. The process component is used to access system processes. After the process component is initialized, you can use this component to obtain information about the currently running process. This information includes the thread set, loaded modules (. dll and. EXE files), and performance information (such as the amount of memory currently used by the process ).
A system process is uniquely identified by its process identifier. Like many windows resources, a process is identified by its handle, which may not be unique on a computer. A handle is a general term used to indicate a resource identifier. Even if the process has exited, the operating system still keeps the process handle, which is accessed through the handle attribute of the process component. Therefore, you can obtain the management information of processes, such as exitcode (usually, or zero indicates success, or non-zero error code) and exittime. Handle is a very valuable resource, So handle leakage is more harmful than memory leakage.
7. Go back to the frmprocessinfo. CS [design] Page and double-click the btngetprocessbyid button. Then the IDE automatically adds the btngetprocessbyid_click event in frmprocessinfo. CS
. In the btngetprocessbyid_click event, enter the following code:
Try
{
String S = "";
System. int32 processid;
Process process;
// Retrieve the additional information about a specific process
Processid = int32.parse (txtprocessid. Text );
Process = system. Diagnostics. process. getprocessbyid (processid );
S = S + "overall priority category of the process:" + convert. tostring (process. priorityclass) + "/R/N ";
S = S + "Number of handles opened by this process:" + process. handlecount + "/R/N ";
S = S + "Main Window title of the process:" + process. mainwindowtitle + "/R/N ";
S = S + "minimum working set size allowed by the process:" + process. minworkingset. tostring () + "/R/N ";
S = S + "Maximum working set size allowed by the process:" + process. maxworkingset. tostring () + "/R/N ";
S = S + "Paging memory size of the process:" + process. pagedmemorysize + "/R/N ";
S = S + "peak paging memory size of the process:" + process. peakpagedmemorysize + "/R/N ";
System. Windows. Forms. MessageBox. Show (s );
}
Catch
{
System. Windows. Forms. MessageBox. Show ("invalid process ID! ");
}
[Note] the int32 value type indicates a signed integer between-2,147,483,648 and + 2,147,483,647.
Int32 provides some methods to compare instances of this type, convert the Instance value to its string representation, and convert the string representation of numbers to instances of this type.
For information about how the format standard code controls the string representation of the value type, see format settings overview.
This type of implementation interfaces are icomparable, iformattable, and iconvertible. Use the convert class for conversion, instead of using this type of iconvertible explicit interface member.
8. The main code is complete. Next, run the "generate"> "generate solution" command in the menu and press F5 to test the program.
The following figure shows the current process list and the basic information of a specific ID process:
It is worth mentioning that the process class has many member variables, which can obtain almost every detail of the process. In the above example, only a few Members are selected for demonstration. If necessary, you can refer to the msdn library to query the process class members for more detailed information, which is not listed here.
The preceding example shows how to use the. NET Framework and C # process classes to obtain process information. Through this example, we can see that C # provides us with a powerful and convenient process class, making development easier and more convenient. This is very helpful for writing efficient and fast application development.
We hope that more friends can provide better comments and suggestions. You are also welcome to share more development and learning experiences.