Process 类
提供对本地和远程进程的访问并使您能够启动和停止本地系统进程。
**命名空间:**System.Diagnostics
**程序集:**System(在 system.dll 中)
语法
声明
Public Class Process
Inherits Component
用法
Dim instance As Process
public class Process : Component
public ref class Process : public Component
public class Process extends Component
public class Process extends Component
备注
提示
应用于此类的 HostProtectionAttribute 属性 (Attribute) 具有以下 Resources 属性 (Property) 值:Synchronization | SharedState | ExternalProcessMgmt | SelfAffectingProcessMgmt。HostProtectionAttribute 并不会影响桌面应用程序(桌面应用程序通常通过双击图标、键入命令或在浏览器中输入 URL 来启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性。
Process 组件提供对正在计算机上运行的进程的访问。用最简短的话来说,进程就是当前运行的应用程序。线程是操作系统向其分配处理器时间的基本单位。线程可执行进程的任何一部分代码,包括当前由另一线程执行的部分。
对于启动、停止、控制和监视应用程序等任务,Process 组件是很有用的工具。使用 Process 组件,可以获取正在运行的进程的列表,或者可以启动新的进程。Process 组件用于访问系统进程。初始化 Process 组件后,可使用该组件来获取有关当前运行的进程的信息。此类信息包括线程集、加载的模块(.dll 和 .exe 文件)和性能信息(如进程当前使用的内存量)。
如果在系统中用引号声明了一个路径变量,则在启动该位置中的任何进程时,必须完全限定该路径。否则,系统将找不到该路径。例如,如果 c:\mypath
不在您的路径中,而您使用引号添加它 (path = %path%;"c:\mypath"
),则在启动 c:\mypath
中的任何进程时,您必须完全限定它们。
进程组件同时获取有关一组属性的信息。Process 组件获取有关任一组的一个成员的信息后,它将缓存该组中其他属性的值,并且在您调用 Refresh 方法之前,不获取有关该组中其他成员的新信息。因此,不保证属性值比对 Refresh 方法的最后一次调用更新。组细分与操作系统有关。
系统进程在系统上由其进程标识符唯一标识。与许多 Windows 资源一样,进程也由其句柄标识,而句柄在计算机上可能不唯一。句柄是表示资源标识符的一般术语。即使进程已退出,操作系统仍保持进程句柄,该句柄通过 Process 组件的 Handle 属性访问。因此,可以获取进程的管理信息,如 ExitCode(通常,或者为零表示成功,或者为非零错误代码)和 ExitTime。句柄是非常有价值的资源,所以句柄泄漏比内存泄漏危害更大。
提示
此类在类级别上包含一个链接要求和一个继承要求,这两个要求应用于所有成员。如果直接调用方或派生类不具有完全信任权限,则会引发 SecurityException。有关安全要求的详细信息,请参见 链接要求 和 继承要求。
示例
下面的示例使用 Process 类的实例启动进程。
Imports System
Imports System.Diagnostics
Imports System.ComponentModel
Namespace MyProcessSample
_
'/ <summary>
'/ Shell for the sample.
'/ </summary>
Class MyProcess
' These are the Win32 error code for file not found or access denied.
Private ERROR_FILE_NOT_FOUND As Integer = 2
Private ERROR_ACCESS_DENIED As Integer = 5
'/ <summary>
'/ Prints a file with a .doc extension.
'/ </summary>
Sub PrintDoc()
Dim myProcess As New Process()
Try
' Get the path that stores user documents.
Dim myDocumentsPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
myProcess.StartInfo.FileName = myDocumentsPath + "\MyFile.doc"
myProcess.StartInfo.Verb = "Print"
myProcess.StartInfo.CreateNoWindow = True
myProcess.Start()
Catch e As Win32Exception
If e.NativeErrorCode = ERROR_FILE_NOT_FOUND Then
Console.WriteLine((e.Message + ". Check the path."))
Else
If e.NativeErrorCode = ERROR_ACCESS_DENIED Then
' Note that if your word processor might generate exceptions
' such as this, which are handled first.
Console.WriteLine((e.Message + ". You do not have permission to print this file."))
End If
End If
End Try
End Sub 'PrintDoc
Public Shared Sub Main()
Dim myProcess As New MyProcess()
myProcess.PrintDoc()
End Sub 'Main
End Class 'MyProcess
End Namespace 'MyProcessSample
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
{
/// <summary>
/// Shell for the sample.
/// </summary>
class MyProcess
{
// These are the Win32 error code for file not found or access denied.
const int ERROR_FILE_NOT_FOUND =2;
const int ERROR_ACCESS_DENIED = 5;
/// <summary>
/// Prints a file with a .doc extension.
/// </summary>
void PrintDoc()
{
Process myProcess = new Process();
try
{
// Get the path that stores user documents.
string myDocumentsPath =
Environment.GetFolderPath(Environment.SpecialFolder.Personal);
myProcess.StartInfo.FileName = myDocumentsPath + "\\MyFile.doc";
myProcess.StartInfo.Verb = "Print";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
catch (Win32Exception e)
{
if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
Console.WriteLine(e.Message + ". Check the path.");
}
else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
{
// Note that if your word processor might generate exceptions
// such as this, which are handled first.
Console.WriteLine(e.Message +
". You do not have permission to print this file.");
}
}
}
public static void Main()
{
MyProcess myProcess = new MyProcess();
myProcess.PrintDoc();
}
}
}
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
// These are the Win32 error code for file not found or access denied.
#define ERROR_FILE_NOT_FOUND 2
#define ERROR_ACCESS_DENIED 5
int main()
{
Process^ myProcess = gcnew Process;
try
{
// Get the path that stores user documents.
String^ myDocumentsPath = Environment::GetFolderPath( Environment::SpecialFolder::Personal );
myProcess->StartInfo->FileName = String::Concat( myDocumentsPath, "\\MyFile.doc" );
myProcess->StartInfo->Verb = "Print";
myProcess->StartInfo->CreateNoWindow = true;
myProcess->Start();
}
catch ( Win32Exception^ e )
{
if ( e->NativeErrorCode == ERROR_FILE_NOT_FOUND )
{
Console::WriteLine( "{0}. Check the path.", e->Message );
}
else
if ( e->NativeErrorCode == ERROR_ACCESS_DENIED )
{
// Note that if your word processor might generate exceptions
// such as this, which are handled first.
Console::WriteLine( "{0}. You do not have permission to print this file.", e->Message );
}
}
}
下面的示例使用 Process 类本身和静态 Start 方法启动进程。
Imports System
Imports System.Diagnostics
Imports System.ComponentModel
Namespace MyProcessSample
_
'/ <summary>
'/ Shell for the sample.
'/ </summary>
Class MyProcess
'/ <summary>
'/ Opens the Internet Explorer application.
'/ </summary>
Public Sub OpenApplication(myFavoritesPath As String)
' Start Internet Explorer. Defaults to the home page.
Process.Start("IExplore.exe")
' Display the contents of the favorites folder in the browser.
Process.Start(myFavoritesPath)
End Sub 'OpenApplication
'/ <summary>
'/ Opens urls and .html documents using Internet Explorer.
'/ </summary>
Sub OpenWithArguments()
' url's are not considered documents. They can only be opened
' by passing them as arguments.
Process.Start("IExplore.exe", "www.northwindtraders.com")
' Start a Web page using a browser associated with .html and .asp files.
Process.Start("IExplore.exe", "C:\myPath\myFile.htm")
Process.Start("IExplore.exe", "C:\myPath\myFile.asp")
End Sub 'OpenWithArguments
'/ <summary>
'/ Uses the ProcessStartInfo class to start new processes, both in a minimized
'/ mode.
'/ </summary>
Sub OpenWithStartInfo()
Dim startInfo As New ProcessStartInfo("IExplore.exe")
startInfo.WindowStyle = ProcessWindowStyle.Minimized
Process.Start(startInfo)
startInfo.Arguments = "www.northwindtraders.com"
Process.Start(startInfo)
End Sub 'OpenWithStartInfo
Shared Sub Main()
' Get the path that stores favorite links.
Dim myFavoritesPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)
Dim myProcess As New MyProcess()
myProcess.OpenApplication(myFavoritesPath)
myProcess.OpenWithArguments()
myProcess.OpenWithStartInfo()
End Sub 'Main
End Class 'MyProcess
End Namespace 'MyProcessSample
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
{
/// <summary>
/// Shell for the sample.
/// </summary>
class MyProcess
{
/// <summary>
/// Opens the Internet Explorer application.
/// </summary>
void OpenApplication(string myFavoritesPath)
{
// Start Internet Explorer. Defaults to the home page.
Process.Start("IExplore.exe");
// Display the contents of the favorites folder in the browser.
Process.Start(myFavoritesPath);
}
/// <summary>
/// Opens urls and .html documents using Internet Explorer.
/// </summary>
void OpenWithArguments()
{
// url's are not considered documents. They can only be opened
// by passing them as arguments.
Process.Start("IExplore.exe", "www.northwindtraders.com");
// Start a Web page using a browser associated with .html and .asp files.
Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");
Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp");
}
/// <summary>
/// Uses the ProcessStartInfo class to start new processes, both in a minimized
/// mode.
/// </summary>
void OpenWithStartInfo()
{
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);
startInfo.Arguments = "www.northwindtraders.com";
Process.Start(startInfo);
}
static void Main()
{
// Get the path that stores favorite links.
string myFavoritesPath =
Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
MyProcess myProcess = new MyProcess();
myProcess.OpenApplication(myFavoritesPath);
myProcess.OpenWithArguments();
myProcess.OpenWithStartInfo();
}
}
}
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
/// <summary>
/// Opens the Internet Explorer application.
/// </summary>
void OpenApplication( String^ myFavoritesPath )
{
// Start Internet Explorer. Defaults to the home page.
Process::Start( "IExplore.exe" );
// Display the contents of the favorites folder in the browser.
Process::Start( myFavoritesPath );
}
/// <summary>
/// Opens urls and .html documents using Internet Explorer.
/// </summary>
void OpenWithArguments()
{
// url's are not considered documents. They can only be opened
// by passing them as arguments.
Process::Start( "IExplore.exe", "www.northwindtraders.com" );
// Start a Web page using a browser associated with .html and .asp files.
Process::Start( "IExplore.exe", "C:\\myPath\\myFile.htm" );
Process::Start( "IExplore.exe", "C:\\myPath\\myFile.asp" );
}
/// <summary>
/// Uses the ProcessStartInfo class to start new processes, both in a minimized
/// mode.
/// </summary>
void OpenWithStartInfo()
{
ProcessStartInfo^ startInfo = gcnew ProcessStartInfo( "IExplore.exe" );
startInfo->WindowStyle = ProcessWindowStyle::Minimized;
Process::Start( startInfo );
startInfo->Arguments = "www.northwindtraders.com";
Process::Start( startInfo );
}
int main()
{
// Get the path that stores favorite links.
String^ myFavoritesPath = Environment::GetFolderPath( Environment::SpecialFolder::Favorites );
OpenApplication( myFavoritesPath );
OpenWithArguments();
OpenWithStartInfo();
}
.NET Framework 安全性
- SecurityPermission 用于调用 Process 的成员。要求值:LinkDemand;命名的权限集:FullTrust。
- SecurityPermission 用于从 Process 类派生。要求值:InheritanceDemand;命名的权限集:FullTrust。
继承层次结构
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Diagnostics.Process
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0
请参见
参考
Process 成员
System.Diagnostics 命名空间
Start
ProcessStartInfo
CloseMainWindow
Kill
ProcessThread