C # calls external programs using the Process Class (GO)

Source: Internet
Author: User

In program development, a program often calls for other programs, and the Process class in C # just provides this functionality. It provides access to local and remote processes and enables you to start and stop local system processes.

First, start the process instance

New Process ();    Try   {       false;        " Test.exe " ;        true ;       Myprocess.start ();   }    Catch (Exception e)   {       Console.WriteLine (e.message);   

The Process.Start method starts (or reuses) the process resource specified by the StartInfo property of this process component and associates it with the component. True if a process resource is started, or false if no new process resource is started (for example, if an existing process is reused).

The Process.startinfo property, Gets or sets the property of the Start method to pass to the Process. StartInfo represents a set of parameters that are used to start a process. When start is called, StartInfo is used to specify the process to start. The only startinfo member that must be set is the FileName property.

Processstartinfo.filename property, Gets or sets the application or document to start. The FileName property does not need to represent an executable file. It can be any file type whose extension is already associated with an application installed on the system.

The Processstartinfo.createnowindow property, Gets or sets a value that indicates whether the process is started in a new window.

Second, turn off the START process

The Process.kill method immediately stops the associated process. Kill forces the process to terminate and the Kill method executes asynchronously. After calling the Kill method, call the WaitForExit method to wait for the process to exit, or check the HasExited property to determine if the process has exited.

Third, call other methods after the process is finished

After invoking a process, we usually need to perform other methods, for example, to determine if the. NET Framework environment is installed and, if not, to continue to run the Setup method after the. NET Framework setup is called when the installer is installed.
Typically, there are two ways to do this:

method One: WaitForExit () method . This method blocks the current process until the running external program exits.

System.Diagnostics.Process Exep = System.Diagnostics.Process.Start (@ "C:\Windows\Notepad.exe  ");   Exep. WaitForExit (); // Critical, wait for external programs to exit before executing    MessageBox.Show ("Notepad.exe Run complete ");

The above code pops up in the dialog box after the Notepad.exe Notepad is finished.

method Two: Exited event . Add an event monitor for an external process, and when you exit, get a notification that this method does not block the current process, and you can handle other things.

New System.Diagnostics.Process ();    @" C:\Windows\Notepad.exe " ;    true ;    New EventHandler (exep_exited);   Exep. Start ();      void exep_exited (object  sender, EventArgs e)   {   MessageBox.Show ("  Notepad.exe Run complete ");   

After the event ends, call the Exited event method.

For the operation of calling an external program in the development of C # program, to determine whether the called EXE file execution end is the most System.Diagnostics.Process class application, one method is WaitForExit (); And the HasExited property, both are designed to determine the execution state of an external program EXE file, and the hasexited=ture indicates the end of execution.

C # calls external programs using the Process Class (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.