Subscribe

RSS Feed (xml)

Start a New Process

The Process class provides a managed representation of an operating system process and provides a simple mechanism through which you can execute both managed and unmanaged applications. The Process class implements four overloads of the Start method, which you use to start a new process. Two of these overloads are static methods that allow you to specify only the name and arguments for the new process. For example, the following statements both execute Notepad in a new process:
// Execute notepad.exe with no command-line arguments.
Process.Start("notepad.exe");

// Execute notepad.exe passing the name of the file to open as a 
// command-line argument.
Process.Start("notepad.exe", "SomeFile.txt");
 
The other two Start method overloads require you to create a ProcessStartInfo object configured with the details of the process you want to run; use of the ProcessStartInfo object provides greater control over the behavior and configuration of the new process.

Properties of the ProcessStartInfo Class
Property
Description
Arguments
The command-line arguments to pass to the new process.
ErrorDialog
If Process.Start can't start the specified process, it will throw a System.ComponentModel.Win32Exception. If ErrorDialog is true, Start displays an error dialog to the user before throwing the exception.
FileName
The name of the application to start. You can also specify any type of file for which you have configured an application association. For example, you could specify a file with a .doc or .xls extension, which would cause Microsoft Word or Microsoft Excel to run.
WindowStyle
A member of the System.Diagnostics.ProcessWindowStyle enumeration, which controls how the window is displayed. Valid values include Hidden, Maximized, Minimized, and Normal.
WorkingDirectory
The fully qualified name of the initial directory for the new process.
When finished with a Process object, you should dispose of it in order to release system resources—call Close, Dispose, or create the Process object within the scope of a using statement. Disposing of a Process object has no effect on the underlying system process, which will continue to run.
The following example uses Process to execute Notepad in a maximized window and open a file named C:\Temp\file.txt. After creation, the example calls the Process.WaitForExit method, which blocks the calling thread until a process terminates or a specified time-out expires.
using System;
using System.Diagnostics;

public class StartProcessExample {

    public static void Main () {

        // Create a ProcessStartInfo object and configure it with the 
        // information required to run the new process.
        ProcessStartInfo startInfo = new ProcessStartInfo();

        startInfo.FileName = "notepad.exe";
        startInfo.Arguments = "file.txt";
        startInfo.WorkingDirectory = @"C:\Temp";
        startInfo.WindowStyle = ProcessWindowStyle.Maximized;
        startInfo.ErrorDialog = true;

        // Create a new Process object.
        using (Process process = new Process()) {

            // Assign the ProcessStartInfo to the Process.
            process.StartInfo = startInfo;

            try {

                // Start the new process.
                process.Start();

                // Wait for the new process to terminate before exiting.
                Console.WriteLine("Waiting 30 seconds for process to" +
                    " finish.");
                process.WaitForExit(30000);

            } catch (Exception ex) {

                Console.WriteLine("Could not start process.");
                Console.WriteLine(ex);
            }
        }

        // Wait to continue.
        Console.WriteLine("Main method complete. Press Enter.");
        Console.ReadLine();
    }
}

No comments:

Post a Comment

Archives

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail