(1) Create an EXE file with startup parameters.
Steps:
1. Define the global private variable: Private string [] S = new string [1]; // here, only one parameter is used for simplicity.
2. initialize startup parameters in the form constructor.
Public form1 (string [] P)
{
Initializecomponent ();
S = P;
}
3. Judge the parameters in the main () function.
Static void main (string [] ARGs)
{
Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault (false );
If (ARGs. Length <= 0)
{
MessageBox. Show ("Enter the startup parameter ");
Application. Exit ();
}
If (ARGs. Length = 1)
{
If (ARGs [0] = "test ")
{
Application. Run (New form1 (ARGs ));
}
Else
{
MessageBox. Show ("Startup parameter error, please input test ");
Application. Exit ();
}
}
}
At this point, the preparation of the EXE file with startup parameters is complete.
(2) Call the EXE file with parameters (call the program just made). I wrote the following simple function:
Public bool startprocess (string filename, string [] ARGs)
{
Try
{
String S = "";
Foreach (string ARG in ARGs)
{
S = S + Arg + "";
}
S = S. Trim ();
Process myprocess = new process ();
Processstartinfo startinfo = new processstartinfo (filename, S );
Myprocess. startinfo = startinfo;
Myprocess. startinfo. useshellexecute = false;
Myprocess. Start ();
Return true;
}
Catch (exception ex)
{
MessageBox. Show ("An error occurred while starting the application! Cause: "+ ex. Message );
}
Return false;
}
Next, call this function at the place where it is called.
Private void button#click (Object sender, eventargs E)
{
String [] Arg = new string [1];
Arg [0] = textbox1.text. Trim ();
Startprocess (@ "E:/zhouxl/C #/fileop/bin/debug/fileop.exe", ARG );
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/vshirleyzhxl/archive/2008/08/03/2760986.aspx