次の方法で共有


Process.StartInfo プロパティ

ProcessStart メソッドに渡すプロパティを取得または設定します。

Public Property StartInfo As ProcessStartInfo
[C#]
public ProcessStartInfo StartInfo {get; set;}
[C++]
public: __property ProcessStartInfo* get_StartInfo();public: __property void set_StartInfo(ProcessStartInfo*);
[JScript]
public function get StartInfo() : ProcessStartInfo;public function set StartInfo(ProcessStartInfo);

プロパティ値

プロセスを起動するときに使用するデータを表す ProcessStartInfo 。これらの引数には、プロセスの起動時に使用する実行可能ファイルまたは文書の名前があります。

例外

例外の種類 条件
ArgumentException StartInfo を指定する値が null 参照 (Visual Basic では Nothing) です。

解説

StartInfo は、プロセスを起動するために使用するパラメータのセットを表します。 Start を呼び出すときは、 StartInfo を使用して、起動するプロセスを指定します。

設定する必要がある唯一の StartInfo メンバは、 FileName プロパティです。 FileName プロパティの指定によるプロセスの起動は、Windows の [ スタート] メニューの [ ファイル名を指定して実行] ダイアログ ボックスへの情報の入力と同様です。そのため、 FileName プロパティで実行可能ファイルを表す必要はありません。システムにインストールされているアプリケーションに関連付けられている拡張子を持つ任意のファイルの種類を指定できます。たとえば、テキスト ファイルをメモ帳などのエディタに関連付けている場合は、 FileName に拡張子 .txt の付いたファイルを使用できます。.doc ファイルを Microsoft Word などのワード プロセッシング ツールに関連付けている場合は、.doc を使用できます。同様に、[ ファイル名を指定して実行] ダイアログ ボックスで実行可能ファイル名は拡張子 .exe が付いているかどうかに関係なく受け入れられるのと同じように、 FileName メンバでも拡張子 .exe は省略できます。たとえば、 FileName プロパティには "Notepad.exe" または "Notepad" を設定できます。

ファイル名が .doc ファイルなど実行可能ファイル以外のファイルの場合は、ファイルに対して実行するアクションを指定する動詞を含めることができます。たとえば、拡張子 .doc で終わるファイルの場合、 Verb に "Print" を設定できます。 Verb プロパティの値を手動で入力する場合、 FileName プロパティで指定するファイル名に拡張子を付ける必要はありません。ただし、使用できる動詞を判断するのに Verbs プロパティを使用する場合は、ファイル拡張子を付ける必要があります。

プロセスを起動するとき、このファイル名が (読み取り専用) MainModule プロパティに設定されるファイルになります。プロセスを起動した後でプロセスに関連付けられた実行可能ファイルを取得するには、 MainModule プロパティを使用します。関連付けられたプロセスがまだ起動されていない Process インスタンスの実行可能ファイルを設定するには、 StartInfo プロパティの FileName メンバを使用します。 StartInfo プロパティのメンバは、プロセスの Start メソッドに渡される引数であるため、関連付けられたプロセスが起動された後で FileName プロパティを変更しても、 MainModule プロパティはリセットされません。これらのプロパティは、関連付けられたプロセスの初期化にだけ使用します。

使用例

[Visual Basic, C#, C++] 実行するファイル、それに対して実行するアクション、およびユーザー インターフェイスを表示するかどうかを指定する値を StartInfo に格納する例を次に示します。

 
Imports System
Imports System.Diagnostics
Imports System.ComponentModel


Namespace MyProcessSample
    _
   '/ <summary>
   '/ Shell for the sample.
   '/ </summary>
   Public 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>
      Public 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

[C#] 
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    /// <summary>
    /// Shell for the sample.
    /// </summary>
    public 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>
        public 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();
        }
    }
}

[C++] 
#using <mscorlib.dll>
#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 = new Process();

    try {
        // Get the path that stores user documents.
        String* myDocumentsPath = 
            Environment::GetFolderPath(Environment::SpecialFolder::Personal);

        myProcess->StartInfo->FileName = String::Concat(myDocumentsPath, S"\\MyFile.doc"); 
        myProcess->StartInfo->Verb = S"Print";
        myProcess->StartInfo->CreateNoWindow = true;
        myProcess->Start();
    } catch (Win32Exception* e) {
        if (e->NativeErrorCode == ERROR_FILE_NOT_FOUND) {
            Console::WriteLine(S"{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(S"{0}. You do not have permission to print this file.", e->Message);
        }
    }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

Process クラス | Process メンバ | System.Diagnostics 名前空間 | Start | FileName