Starts a process specified by the StartInfo property of this Process component and associates it with the Process. If a process resource is reused rather than started, the reused process is associated with this Process component.
[Visual Basic] Overloads Overridable Public Function Start() As Boolean [C#] public virtual bool Start(); [C++] public: virtual bool Start(); [JScript] public function Start() : Boolean;
true if a process resource is started; false if no new process resource is started (for example, if an existing process is reused).
Exception Type | Condition |
---|---|
ArgumentException | No file name was specified in the Process component's StartInfo. |
Win32Exception | There was an error opening the associated file. |
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.
Note 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 (in Visual Basic Shared) method. You must call it on an instance of the Process class. Before calling Start, you must first specify StartInfo property information for this Process because that information is used to determine the process resource to start.
The other overloads of the Start method are static (Shared) 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 on the Process class itself and a new Process component is created if the process was started (or a null reference (in Visual Basic Nothing) is returned if a process was reused). The process resource is automatically associated with the new Process component returned by the call to 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 launched by setting appropriate values for the StartInfo. The only StartInfo property that must be set is the FileName. The FileName does not have to be an executable. It can be any file type whose extension has been associated with an application installed on the system, for example ".txt" if you have associated text files with an editor like Notepad, or ".doc" if you have .doc files associated with a word processing tool like 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 or Verbs members of the StartInfo property. For other types of files, you can specify command line arguments when launching them from the Run dialog box of the Start menu. 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.
See the ProcessStartInfo class for details about process start information.
Whenever you use Start to start a process, you must be sure to close it or you risk losing system resources. Close processes using CloseMainWindow or Kill. CloseMainWindow is used to close processes that contain a graphical interface, while Kill must be used for non-graphical applications that do not have a message pump to handle a windows Close request.
Note Currently, all processes started using a Process component open in a command window. They contain a message loop and you can attempt to close them using CloseMainWindow. This will change in a future release. Depending on how the non-graphical interface process is coded, CloseMainWindow could fail and Kill could be necessary in order to stop the process.
The following example demonstrates starting one of four processes (Word, Excel, Internet Explorer, or Notepad) depending on information regarding a document passed into the procedure, and assigning information to an existing process object which is then started by the procedure.
StartProcess takes two parameters. The first parameter is the name of a document (in this case, 'document' is a generic term for any viewable file, for example MyDoc.doc or MyFile.txt) and the second is the extension of the document (.doc, .txt, .htm., or .xls). Using the extension, the StartProcess procedure associates an application with the Process component's StartInfo. Then the document itself is assigned as an argument with which the application is opened. In the case of Excel or Word, the document is opened in order to be printed. In the case of Notepad or Internet Explorer, the document is only loaded into the application's window.
The fileName and arguments are the same that would type into the Run dialog box of the Start menu. A window displays the document passed into StartProcess as the docName
parameter.
[Visual Basic]
Private Sub StartProcess(ByVal fileName As String, ByVal extension As String) 'Declare and instantiate a new process component. Dim myproc As System.Diagnostics.Process myproc = New System.Diagnostics.Process 'Do not receive an event when the process exits. myproc.WatchForExit = False 'Use the extenstion to determine which application to open. Select Case extension Case "doc" '.doc files open in Word. myproc.StartInfo.FileName = "winword" 'Print the file. myproc.StartInfo.Verb = "print" Case "xls" '.xls files open in Excel. myproc.StartInfo.FileName = "excel" 'Print the file. myproc.StartInfo.Verb = "print" Case "htm" '.htm files open in the browser. myproc.StartInfo.FileName = "iexplore" Case "txt" '.txt files open in Notepad. myproc.StartInfo.FileName = "notepad" End Select 'Assign the document the file should open. myproc.StartInfo.Arguments = fileName 'Start the application and assign it to the process component. myproc.Start End Sub
Process Class | Process Members | System.Diagnostics Namespace | Process.Start Overload List | StartInfo | FileName | ProcessStartInfo | CloseMainWindow | Kill