C # Call the EXE file with parameters and the EXE file with startup parameters

Source: Internet
Author: User

(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

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.