Windows processes

I. Overview

A Process component provides access to the computer running the process. Process, in the simplest terms, the application is running. Provide access to local and remote processes and enables you to start and stop local system processes.

Attributes

  • Id Gets a unique identifier for the associated process.
  • ProcessName get the name of the process.
  • MachineName Gets the name of the computer on which the associated process is running.
  • SessionId obtain a Terminal Services session identifier for the associated process.
  • StandardError acquisition stream application for reading error output.
  • StandardInput acquisition stream input for writing applications.
  • Get StandardOutput for streaming applications to read text output.
  • Gets or sets the property StartInfo Process to pass to the Start () method.
  • StartTime Gets the associated process time to start.
  • Threads obtain a set of threads running in the associated process.
  • Handle Gets the native handle the associated process.
  • MainWindowHandle get the window handle of the main window of the associated process.
  • MainWindowTitle acquisition process of the main window title.   
  • MainModule acquisition main module for the associated process.
  • Modules acquisition module is loaded by the associated process.

method

  • GetCurrentProcess () to obtain a new Process component and associates it with the currently active processes.
  • GetProcessById (Int32) Returns a new Process component (given identifier of a process on the local computer).
  • GetProcesses () Creates a new Process component for each process resource on the local computer.
  • GetProcessesByName (String) Creates a new array of Process components, and associate them with all processes on the local computer to share the specified process name of the resource.
  • Kill () immediately cease the associated processes.
  • Start () to start (or reuse) the process resource StartInfo property of this Process component specified, and associated with the component.
  • WaitForExit (Int32) indicates the Process component to wait for the associated process to exit within a specified number of milliseconds.

Second, the use

1, and the establishment of the destruction process System.Diagnosties

System.Diagnostics.Process p=Process.Start("notepad.exe","File.txt");
Thread.Sleep(2000);
p.Kill();

2, processes running in the background:

PSI = ProcessStartInfo new new ProcessStartInfo ( " cmd.exe " ); 
psi.Arguments = @ " / c Copy c: \ 1.txt lpt1 " ; 
psi.CreateNoWindow = to true ; 
psi.UseShellExecute = false ; // default is true, use shell to start the process. Otherwise, the process is created directly from the executable file. 
P = Process the Process.Start (PSI);
 IF (! P.WaitForExit ( 1000 * . 7 )) // Start external program, waits for it to exit 
{ 
    p.Kill (); 
} 
p.Close ();

or

ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");//FileName
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;//将此属性设置为false,就能重定向进程。。。
psi.UseShellExecute = false;//
Process p = Process.Start(psi);
StreamWriter sw = p.StandardInput;
StreamReader sr = p.StandardOutput;
sw.WriteLine(@"/c copy c:\1.txt lpt1");
sw.Close();
Console.Write(sr.ReadToEnd());
sr.Close();
p.Close();

3. Start external program

System.Diagnostics.Process.Start ( " explorer.exe " , @ " c: / " ); // open Explorer with C: /   
System.Diagnostics.Process.Start ( " rundll32.exe " , @ " shell32. DLL, Control_RunDLL appwiz.cpl ,, 0 " ); // open the" Add or Remove programs "panel:

4, the enumeration process is running in the computer

var pList = Process.GetProcesses().OrderBy(x => x.Id).Take(10);
foreach (var p in pList)
{
    Console.WriteLine(string.Format("ProcessID is {0} \t ProcessName is {1}", p.Id, p.ProcessName));
}

5, the process of obtaining a plurality of modules

var mList = Process.GetCurrentProcess().Modules;
foreach (ProcessModule m in mList)
{
    Console.WriteLine(string.Format("ModuleName is {0} \t ModuleURL  is {1} \t ModuleVersion is {2}", m.ModuleName, m.FileName, m.FileVersionInfo.FileVersion));
}

Guess you like

Origin www.cnblogs.com/springsnow/p/11268396.html