Process クラス
アセンブリ: System (system.dll 内)


![]() |
---|
このクラスに適用される HostProtectionAttribute 属性の Resources プロパティの値は、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 メソッドを最後に呼び出したときよりも新しいという保証はありません。グループの内訳は、オペレーティング システムに依存します。
システム プロセスは、プロセス ID によってシステム上で一意に識別されます。多くの Windows リソースと同じように、プロセスもハンドルで識別されます。しかし、ハンドルはコンピュータで一意でない可能性があります。ハンドルとは、リソース識別子を一般的に表現した用語です。オペレーティング システムは、プロセス ハンドルを維持します。プロセスが終了していても、Process コンポーネントの Handle プロパティを通じて、プロセス ハンドルにアクセスできます。そのため、ExitCode (正常終了した場合はゼロ、それ以外の場合は 0 以外のエラー コード) や ExitTime など、プロセスの管理情報を取得できます。ハンドルは貴重なリソースであるため、ハンドルのリークはメモリのリークよりも有害です。

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(); }


System.MarshalByRefObject
System.ComponentModel.Component
System.Diagnostics.Process


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からProcess クラスを検索する場合は、下記のリンクをクリックしてください。

- Process クラスのページへのリンク