1 Process
简单实例:
//
Execute notepad.exe with no command-line arguments.
Process.Start(
"
notepad.exe
"
);
//
Execute notepad.exe passing the name of the file to open as a
//
command-line argument.
Process.Start(
"
notepad.exe
"
,
"
SomeFile.txt
"
);
Properties of the ProcessStartInfo Class
Property
|
Description
|
Arguments
|
The command-line arguments to pass to the new process.
|
ErrorDialog
|
If Process.Start can't start the specified process, it will throw a System.ComponentModel.Win32Exception. If ErrorDialog is true, Start displays an error dialog to the user before throwing the exception.
|
FileName
|
The name of the application to start. You can also specify any type of file for which you have configured an application association. For example, you could specify a file with a .doc or .xls extension, which would cause Microsoft Word or Microsoft Excel to run.
|
WindowStyle
|
A member of the System.Diagnostics.ProcessWindowStyle enumeration, which controls how the window is displayed. Valid values include Hidden, Maximized, Minimized, and Normal.
|
WorkingDirectory
|
The fully qualified name of the initial directory for the new process.
|
进程的启动:
The following example uses Process to execute Notepad in a maximized window and open a file named C:\Temp\file.txt. After creation, the example calls the Process.WaitForExit method, which blocks the calling thread until a process terminates or a specified time-out expires.
using System;
using System.Diagnostics;
public class StartProcessExample {
public static void Main () {
// Create a ProcessStartInfo object and configure it with the
// information required to run the new process.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "notepad.exe";
startInfo.Arguments = "file.txt";
startInfo.WorkingDirectory = @"C:\Temp";
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.ErrorDialog = true;
// Create a new Process object.
using (Process process = new Process()) {
// Assign the ProcessStartInfo to the Process.
process.StartInfo = startInfo;
try {
// Start the new process.
process.Start();
// Wait for the new process to terminate before exiting.
Console.WriteLine("Waiting 30 seconds for process to" +
" finish.");
process.WaitForExit(30000);
} catch (Exception ex) {
Console.WriteLine("Could not start process.");
Console.WriteLine(ex);
}
}
// Wait to continue.
Console.WriteLine("Main method complete. Press Enter.");
Console.ReadLine();
}
}
进程终止:
Methods for Obtaining Process ReferencesMethod | Description |
---|
GetCurrentProcess | Returns a Process object representing the currently active process. |
GetProcessById | Returns a Process object representing the process with the specified ID. |
GetProcesses | Returns an array of Process objects representing all currently active processes. |
GetProcessesByName | Returns an array of Process objects representing all currently active processes with a specified friendly name. The friendly name is the name of the executable excluding file extension or path; for example, notepad or calc. |
The following example starts a new instance of Notepad, waits five seconds, and then terminates the Notepad process. The example first tries to terminate the process using CloseMainWindow. If CloseMainWindow returns false, or the Notepad process is still running after CloseMainWindow is called, the example calls Kill and forces the Notepad process to terminate; you can force CloseMainWindow to return false by leaving the File Open dialog box open.
using System;
using System.Threading;
using System.Diagnostics;
public class TerminateProcessExample {
public static void Main () {
// Create a new Process and run notepad.exe.
using (Process process = Process.Start("notepad.exe")) {
// Wait for 5 seconds and terminate the notepad process.
Console.WriteLine("Waiting 5 seconds before terminating" +
" notepad.exe.");
Thread.Sleep(5000);
// Terminate notepad process.
Console.WriteLine("Terminating Notepad with CloseMainWindow.");
// Try to send a close message to the main window.
if (!process.CloseMainWindow()) {
// Close message did not get sent - Kill Notepad.
Console.WriteLine("CloseMainWindow returned false - " +
" terminating Notepad with Kill.");
process.Kill();
} else {
// Close message sent successfully; wait for 2 seconds
// for termination confirmation before resorting to Kill.
if (!process.WaitForExit(2000)) {
Console.WriteLine("CloseMainWindow failed to" +
" terminate - terminating Notepad with Kill.");
process.Kill();
}
}
}
// Wait to continue.
Console.WriteLine("Main method complete. Press Enter.");
Console.ReadLine();
}
}