Get process information with C #

xiaoxiao2021-03-06  100

This article will illustrate how to use C # to get the process of currently active state and read the basic information of a particular ID process.

Now there are many softwares with the function of reading the system process. It is more famous for Customizer XP, which is a software used to set up a Windows XP system. This software has a function that is capable of opening or shutting down the system running. Of course, this article did not make Customizer XP as beautiful and refined, but I would like to know the basic method, making such a function is very simple.

This example uses the .NET Framework's Process class to get information about the activation process, give a list of current activation processes, and some basic information can be displayed.

Production process

First, run Visual Studio.NET, create a new Visual C # .NET project, here is Type ProcessInfo. Second, in the Solution Explorer, the FORM1.CS is renamed to frMProcessInfo.cs (this is optional). Third, change the text attribute of Form1 to Process Information, font is changed to Verdana, 9pt (this is optional). Fourth, add three controls on the form: two buttons and a TextBox. Modify the properties of the control, as follows: Control Name Properties TEXT Property Button1 BtngetProcessList Click here to get the current activation process list Button 2 btNgetProcessByID get the process of the specified ID: TextBox TxtProcessID N / A Adjust the location of each control, at this time the user interface is similar The following figure:

5. Go to the frmProcessInfo.cs code editor. Add one of the following lines to the file header:

Using system.diagnostics; [Note] System.Diagnostics namespace provides specific classes so that you can interact with system processes, event logs, and performance counters.

6. Double-click the BTNGETPROCESSLIST button in frMProcessInfo.cs [design]. The system is automatically added to the BTNGETPROCESSLIST_CLICK event. Fill in 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

string s = ""; System.Int32 processid; Process process; // Retrieve the additional information about a specific processprocessid = Int32.Parse (txtProcessID.Text); process = System.Diagnostics.Process.GetProcessById (processid); s = s "The overall priority category of this process:" Convert.TOSTRING (Process.PriorityClass) "/ r / n"; s = s "The number of handles opened by this process:" process.handlecount "/ R / n "; s = s " The main window title of the process: " process.mainwindowtitle " / r / n "; s = s " The minimum working set size allowed by this process: " process.minworkingset.tostring () "/ r / n"; s = s "The maximum work set size allowed by this process:" process.maxworkingSet.tostring () "/ r / n"; s = s "Pipe of this process Memory size: " process.pagedMemorySize " / r / n "; s = s " The peak paging memory size of this process: " process.peptionMemorySize " /R/N";system.windows.Forms.MessageBox. Show (s);} catch {System.windows.Forms.MessageBox.show ("illegal process ID!");} [Note] INT32 value type indicates that the value is between -2, 147, 483, 648 to 2, 147, 483, 647 Symbol integer. INT32 provides some ways to compare this type of instance, convert the value of the instance to its String representation and instances of converting the digital string representation into this type. For information on how the format specification code controls the string representation of the value type, see the format setting overview. This type implements interface iComparable, IFORMATTABLE, and ICONVERTIBLE. Conversion using the Convert class instead of using this type of iconvertible explicit interface member implementation. Eight, the main code we have filled out. After using the menu "Generate"> Generate Solution command, press F5 to test the program.

The following is the current process list and the basic information of a particular ID process:

It is worth mentioning that the Process class has many member variables that can get almost every detail of the process. In the above example, only a few members are selected simply. If there is a need in the development, you can refer to MSDN Library to query the Process class member to obtain more detailed information, here is not one listed.

转载请注明原文地址:https://www.9cbs.com/read-123730.html

New Post(0)