Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Process Class
Process Methods
Start Method
 Start Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Process..::.Start Method

Starts (or reuses) the process resource that is specified by the StartInfo property of this Process component and associates it with the component.

Namespace:  System.Diagnostics
Assembly:  System (in System.dll)
Visual Basic (Declaration)
Public Function Start As Boolean
Visual Basic (Usage)
Dim instance As Process
Dim returnValue As Boolean

returnValue = instance.Start()
C#
public bool Start()
Visual C++
public:
bool Start()
JScript
public function Start() : boolean

Return Value

Type: System..::.Boolean
true if a process resource is started; false if no new process resource is started (for example, if an existing process is reused).
ExceptionCondition
InvalidOperationException

No file name was specified in the Process component's StartInfo.

-or-

The ProcessStartInfo..::.UseShellExecute member of the StartInfo property is true while ProcessStartInfo..::.RedirectStandardInput, ProcessStartInfo..::.RedirectStandardOutput, or ProcessStartInfo..::.RedirectStandardError is true.

Win32Exception

There was an error in opening the associated file.

ObjectDisposedException

The process object has already been disposed.

Use this overload to start a process resource and associate it with the current Process component. The return value true indicates that a new process resource was started. If the process resource specified by the FileName member of the StartInfo property is already running on the computer, no additional process resource is started. Instead, the running process resource is reused and false is returned.

You can start a ClickOnce application by specifying the location (for example, a Web address) from which you originally installed the application. Do not start a ClickOnce application by specifying its installed location on your hard drive.

NoteNote:

If you are using Visual Studio, this overload of the Start method is the one that you insert into your code after you drag a Process component onto the designer. Use the Properties window to expand the StartInfo category and write the appropriate value into the FileName property. Your changes will appear in the form's InitializeComponent procedure.

This overload of Start is not a static method. You must call it for an instance of the Process class. Before calling Start, you must first specify StartInfo property information for this Process instance, because that information is used to determine the process resource to start.

The other overloads of the Start method are static members. You do not need to create an instance of the Process component before you call those overloads of the method. Instead, you can call Start for the Process class itself, and a new Process component is created if the process was started. Or, nullNothingnullptra null reference (Nothing in Visual Basic) is returned if a process was reused. The process resource is automatically associated with the new Process component that is returned by the Start method.

The StartInfo members can be used to duplicate the functionality of the Run dialog box of the Windows Start menu. Anything that can be typed into a command line can be started by setting the appropriate values in the StartInfo property. The only StartInfo property that must be set is the FileName property. The FileName property does not have to be an executable file. It can be of any file type for which the extension has been associated with an application that is installed on the system. For example, the FileName property can have a .txt extension if you have associated text files with an editor, such as Notepad, or it can have a .doc extension if you have associated.doc files with a word processing tool, such as Microsoft Word.

In the command line, you can specify actions to take for certain types of files. For example, you can print documents or edit text files. Specify these actions using the Verb member of the StartInfo property. For other types of files, you can specify command-line arguments when you start the file from the Run dialog box. For example, you can pass a URL as an argument if you specify your browser as the FileName. These arguments can be specified in the StartInfo property's Arguments member.

If you have a path variable declared in your system using quotes, you must fully qualify that path when starting any process found in that location. Otherwise, the system will not find the path. For example, if c:\mypath is not in your path, and you add it using quotation marks: path = %path%;"c:\mypath", you must fully qualify any process in c:\mypath when starting it.

NoteNote:

ASP.NET Web page and server control code executes in the context of the ASP.NET worker process on the Web server. If you use the Start method in an ASP.NET Web page or server control, the new process executes on the Web server with restricted permissions. The process does not start in the same context as the client browser, and does not have access to the user desktop.

Whenever you use Start to start a process, you might need to close it or you risk losing system resources. Close processes using CloseMainWindow or Kill.

A note about apartment states in managed threads is necessary here. When UseShellExecute is true on the process component's StartInfo property, make sure you have set a threading model on your application by setting the attribute [STAThread] on the main() method. Otherwise, a managed thread can be in an unknown state or put in the MTA state, the latter of which conflicts with UseShellExecute being true. Some methods require that the apartment state not be unknown. If the state is not explicitly set, when the application encounters such a method, it defaults to MTA, and once set, the apartment state cannot be changed. However, MTA causes an exception to be thrown when the operating system shell is managing the thread.

The following example uses an instance of the Process class to start a process.

Visual Basic
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
C#
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();
        }
    }
}
Visual C++
#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 );
      }
   }

}

  • LinkDemand 

    for full trust for the immediate caller. This member cannot be used by partially trusted code.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Wait for Process to Exit and Get Result      kuke ... Thomas Lee   |   Edit   |   Show History
To wait for the process to exit and get the result, do the following:
// Create the process. 
using (Process process = Process.Start("AppPath\Name", "Command Line Parameters"))
{
// Wait for the process to complete.
process.WaitForExit();

// Access the ExitCode property here to get the exit code from the application you ran.
}

Props to Nicholas Paldino's post.

WaitForExit Help      Jeff Fischer ... Thomas Lee   |   Edit   |   Show History

Refer to my blog for very helpful advice on Process WaitForExit examples..

Jeff Fischer

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker