Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

Process.Start Method (ProcessStartInfo)

.NET Framework 1.1

Starts the process resource that is specified by the parameter containing process start information (for example, the file name of the process to start) and associates the resource with a new Process component.

[Visual Basic]
Overloads Public Shared Function Start( _
   ByVal startInfo As ProcessStartInfo _
) As Process
[C#]
public static Process Start(
 ProcessStartInfo startInfo
);
[C++]
public: static Process* Start(
 ProcessStartInfo* startInfo
);
[JScript]
public static function Start(
   startInfo : ProcessStartInfo
) : Process;

Parameters

startInfo
The ProcessStartInfo that contains the information that is used to start the process, including the file name and any command-line arguments.

Return Value

A new Process component that is associated with the process resource, or a null reference (Nothing in Visual Basic) if no process resource is started (for example, if an existing process is reused).

Exceptions

Exception Type Condition
InvalidOperationException No file name was specified in the startInfo parameter's FileName property.

-or-

The ProcessStartInfo.UseShellExecute member of the startInfo parameter is true while ProcessStartInfo.RedirectStandardInput, ProcessStartInfo.RedirectStandardOutput, or ProcessStartInfo.RedirectStandardError is true.

ArgumentException The startInfo parameter is a null reference (Nothing in Visual Basic).
Win32Exception There was an error in opening the associated file.
ObjectDisposedException The process object has already been disposed.

Remarks

Use this overload to start a process resource by specifying a ProcessStartInfo instance. The overload associates the resource with a new Process component. If the process is already running, no additional process resource is started. Instead, the existing process resource is reused and no new Process component is created. In such a case, instead of returning a new Process component, Start returns a null reference (Nothing in Visual Basic) to the calling procedure.

This overload lets you start a process without first creating a new Process instance. Using this overload with a ProcessStartInfo parameter is an alternative to the explicit steps of creating a new Process instance, setting its StartInfo properties, and calling Start for the Process instance.

Using a ProcessStartInfo instance as the parameter lets you call Start with the most control over what is passed into the call to start the process. If you need to pass only a file name or a file name and arguments, it is not necessary to create a new ProcessStartInfo instance, although that is an option. The only StartInfo property that must be set is the FileName property. The FileName property does not need to represent 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.

Unlike the other overloads, the overload of Start that has no parameters is not a static (Shared in Visual Basic) member. Use that overload when you have already created a Process instance, specified start information (including the file name), and want to start a process resource and associate it with the existing Process instance. Use one of the static (Shared in Visual Basic) overloads when you want to create a new Process component rather than start a process for an existing component. Both this overload and the overload that has no parameters allow you to specify the start information for the process resource by using a ProcessStartInfo instance.

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

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 startInfo parameter, 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.

Example

[Visual Basic, C#, C++] The following example first spawns an instance of Internet Explorer and displays the contents of the favorites folder in the browser. It then starts some other instances of IE and displays some specific pages or sites. Finally it starts IE with the window being minimized while navigating to a specific site.

[Visual Basic] 
Imports System
Imports System.Diagnostics
Imports System.ComponentModel


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

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

namespace MyProcessSample
{
    /// <summary>
    /// Shell for the sample.
    /// </summary>
    public class MyProcess
    {
       
        /// <summary>
        /// Opens the Internet Explorer application.
        /// </summary>
        public 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>
        public 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>
        public void OpenWithStartInfo()
        {
            
            ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
            startInfo.WindowStyle = ProcessWindowStyle.Minimized;
            
            Process.Start(startInfo);
            
            startInfo.Arguments = "www.northwindtraders.com";
            
            Process.Start(startInfo);
            
        }

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

               }    
    }
}

[C++] 
#using <mscorlib.dll>
#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(S"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(S"IExplore.exe", S"www.northwindtraders.com");

    // Start a Web page using a browser associated with .html and .asp files.
    Process::Start(S"IExplore.exe", S"C:\\myPath\\myFile.htm");
    Process::Start(S"IExplore.exe", S"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(S"IExplore.exe");
    startInfo->WindowStyle = ProcessWindowStyle::Minimized;

    Process::Start(startInfo);

    startInfo->Arguments = S"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();
}    

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

Process Class | Process Members | System.Diagnostics Namespace | Process.Start Overload List | StartInfo | FileName | ProcessStartInfo | CloseMainWindow | Kill

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.