Parse C to kill the process with the Process class and perform an in depth analysis of the command

  • 2020-05-10 18:46:06
  • OfStack

c#, process class
1. Get the user name of the process by the process name?
You need to add a reference to System.Management.dll

using System.Diagnostics;
using System.Management;
static void Main(string[] args)
{
foreach (Process p in Process.GetProcesses())
{
Console.Write(p.ProcessName);
Console.Write("----");
Console.WriteLine(GetProcessUserName(p.Id));
}
Console.ReadKey();
}
private static string GetProcessUserName(int pID)
{
string text1 = null;
SelectQuery query1 = new SelectQuery("Select * from Win32_Process WHERE processID=" + pID);
ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(query1);
try
{
foreach (ManagementObject disk in searcher1.Get())
{
ManagementBaseObject inPar = null;
ManagementBaseObject outPar = null;
inPar = disk.GetMethodParameters("GetOwner");
outPar = disk.InvokeMethod("GetOwner", inPar, null);
text1 = outPar["User"].ToString();
break;
}
}
catch
{
text1 = "SYSTEM";
}
return text1;
}

The process name is p.ProcessName
2. Get the process
using System.Diagnostics;
The System.Diagnostics namespace provides specific classes that allow you to interact with system processes, event logs, and performance counters.

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";
}
System.Windows.Forms.MessageBox.Show(str);
txtProcessID.Text = processes[0].Id.ToString();
// Displays process-related information 
string s = "";
System.Int32 processid;
Process process;
processid = Int32.Parse(txtProcessID.Text);
process = System.Diagnostics.Process.GetProcessById(processid);
s = s + " The overall priority category of the process :" + Convert.ToString(process.PriorityClass) + " \r\n";
s = s + " The number of handles opened by the 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 the process :" + process.MinWorkingSet.ToString() + " \r\n";
s = s + " The maximum working set size allowed by the process :" + process.MaxWorkingSet.ToString() + " \r\n";
s = s + " The paging memory size of the process :" + process.PagedMemorySize + "\r\n";
s = s + " The peak paging memory size of the process :" + process.PeakPagedMemorySize + "\r\n";
System.Windows.Forms.MessageBox.Show(s);
}
catch
{
System.Windows.Forms.MessageBox.Show(" Illegal process ID ! ");
}

The Int32 value type represents a signed integer with a value between -2,147,483,648 and +2,147,483,647.
Int32 provides methods to compare instances of the type, convert the value of the instance to its String representation, and convert the String representation of the number to an instance of the type.
For information on how the format specification code controls the String representation of a value type, see formatting overview.
This type implements interfaces IComparable, IFormattable, and IConvertible. The conversion is done using the Convert class instead of the explicit IConvertible interface member implementation of this type.
It is worth mentioning that the Process class has many member variables that can capture almost every detail of the process. The example above simply selects a few members to demonstrate. If necessary in development, you can refer to MSDN Library and query the Process class members for more detailed information, which is not listed here.
3. Kill the process

private void button2_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
try
{
string proName = listView1.SelectedItems[0].Text;
Process[] p = Process.GetProcessesByName(proName);
p[0].Kill();
MessageBox.Show(" Process closed successfully! ");
GetProcess();
}
catch
{
MessageBox.Show(" This process could not be shut down! ");
}
}
else
{
MessageBox.Show(" Select the process to terminate !");
}
}

4. C# USES the process class to call external programs and execute the dos command

private string RunCmd(string command)
{
// The instance 1 a Process Class, start 1 Independent processes 
Process p = new Process();
//Process Class has 1 a StartInfo attribute 
// Program name 
p.StartInfo.FileName = "cmd.exe";
// Set program execution parameters  
p.StartInfo.Arguments = "/c " + command;
// Shut down Shell The use of  
p.StartInfo.UseShellExecute = false; 
// Redirect standard input  
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
// Redirect error output  
p.StartInfo.RedirectStandardError = true; 
// Set not to display a window 
p.StartInfo.CreateNoWindow = true; 
// Start the 
p.Start(); 
// You can also enter commands to execute in this way 
// But remember to add Exit Otherwise the 1 It will crash when the stroke is executed 
//p.StandardInput.WriteLine(command);
//p.StandardInput.WriteLine("exit"); 
// Get command execution results from the output stream 
return p.StandardOutput.ReadToEnd();
}


Related articles: