Reference namespace first:Using system. diagnostics;
Example:
// Open Baidu in the existing window
System. Diagnostics. process. Start ("http://www.baidu.com ");
// Open Baidu in a new window
Using system. diagnostics;
Process PS = new process ();
String yoururl = "http://www.baidu.com";
PS. startinfo. filename = "iexplore.exe ";
PS. startinfo. Arguments = yoururl;
PS. Start ();
1. Use of the Process class
Start starts a process and associates it with the process class.
Kill immediately shut down the process
Waitforexit: waiting for the exit of the associated process
Close to release all processes associated with this
/*
* Created by sharpdevelop.
* User: Administrator
* Date: 2007-6-17
* Time: 16: 20
*
* To change this template use tools | options | coding | edit standard headers.
*/
Using system;
Using system. Collections. Generic;
Using system. drawing;
Using system. Windows. forms;
// Process class namespace
Using system. diagnostics;
Namespace Process
{
/// <Summary>
/// Description of mainform.
/// </Summary>
Public partial class mainform
{
[Stathread]
Public static void main (string [] ARGs)
{
Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault (false );
Application. Run (New mainform ());
}
Public mainform ()
{
//
// The initializecomponent () call is required for Windows Forms designer support.
//
Initializecomponent ();
//
// Todo: Add constructor code after the initializecomponent () call.
//
}
// Start the IE homepage http://www.baidu.com/
Void button1click (Object sender, system. eventargs E)
{
Process. Start ("iexplore.exe", "http://www.baidu.com /");
}
// Start the Resource Manager
Void button2click (Object sender, system. eventargs E)
{
Process. Start ("assumer.exe ");
}
// Start excel in the office
Void button3click (Object sender, system. eventargs E)
{
Process. Start ("excel.exe ");
}
// Start the Windows player
Void button4click (Object sender, system. eventargs E)
{
Process. Start ("dvdplay.exe ");
}
}
}
Source code download: http://download.csdn.net/source/195507
Http://download1.csdn.net/down3/20070617/17164940911.rar with C # to achieve the same effect, found C # itself convenient process thread mechanism makes the work becomes extremely simple, easily record.
2. First, we can get the output interface by setting the process class. The Code is as follows:
Process proc = new process ();
Proc. startinfo. filename = strscript;
Proc. startinfo. workingdirectory = strdirectory;
Proc. startinfo. createnowindow = true;
Proc. startinfo. useshellexecute = false;
Proc. startinfo. redirectstandardoutput = true;
Proc. Start ();
Then set the thread to continuously read the output string:
Eventoutput = new autoresetevent (false );
Autoresetevent [] events = new autoresetevent [1];
Events [0] = m_eventoutput;
M_threadoutput = new thread (New threadstart (displayoutput ));
M_threadoutput.start ();
Waithandle. waitall (events );
The thread functions are as follows:
Private void displayoutput ()
{
While (m_procscript! = NULL &&! M_procscript.hasexited)
{
String strline = NULL;
While (strline = m_procscript.standardoutput.readline ())! = NULL)
{
M_txtoutput.appendtext (strline + "\ r \ n ");
M_txtoutput.selectionstart = m_txtoutput.text.length;
M_txtoutput.scrolltocaret ();
}
Thread. Sleep (100 );
}
M_eventoutput.set ();
}
Note that the following statements are used to make textbox display always the latest, while appendtext does not use + =, it is because + = causes the display of the entire textbox to flash the entire display area.
M_txtoutput.appendtext (strline + "\ r \ n ");
M_txtoutput.selectionstart = m_txtoutput.text.length;
M_txtoutput.scrolltocaret ();
In order not to block the main thread, you can put the whole process in one thread or another.
3. Methods for BAT file control parameters:
Write your net use \ 172.16.17.1/User: Username Password to the BAT file, and then run the following code.
System. Diagnostics. Process = new system. Diagnostics. Process ();
Process. startinfo. createnowindow = false;
Process. startinfo. filename = "d :\\ netuse. Bat ";
Process. Start ();
Program Control Parameter Method:
System. Diagnostics. processstartinfo psi =
New system. Diagnostics. processstartinfo ();
// Prompt
PSI. filename = @ "C: \ windows \ system32 \ cmd.exe"; // path for the CMD prompt
PSI. Arguments = @ "net use \ 172.16.17.1/User: Username Password ";
PSI. windowstyle = system. Diagnostics. processwindowstyle. hidden;
System. Diagnostics. process. Start (PSI );
Use the process to start cmd.exe
An issue about using the process class to run ShellExecute (click to view references)
Only ShellExecute on the stathread can ensure that the job is correct. This problem is not taken into account in the implementation of process, so using the process class to run ShellExecute may cause errors. If you cannot guarantee the apartmentstate of the thread that calls process. Start, you can use the following code to avoid this problem:
Using system;
Using system. Threading;
Public class Foo {
Public static void Openurl (){
System. Diagnostics. process. Start (@ "http://www.google.com ");
}
Public static void main (){
Threadstart openurldelegate = new threadstart (FOO. Openurl );
Thread mythread = new thread (openurldelegate );
Mythread. setapartmentstate (apartmentstate. Sta );
Mythread. Start ();
Mythread. Join ();
}
}