简体   繁体   中英

Start another EXE with command line parameters in C#

I want do start an EXE file with Parameters.

I tried with Button1 to start a consoleapp (works). With Button2 i tried to open the same consoleapp with Parameters (works). With Button3 i tried to open the WindowsFromsApplication with Parameters. The WindowsFromsApplication does not run and casts a WindowsMessage, that it can't be opend (but not in the debugmode).

If I use same WindowsFromsApplication wit a desktop shortcut and parameters it works.

What is wrong my solution?

 public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Process.Start(Application.StartupPath + "\\ParamTest1.exe");
    }

    private void button2_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Process.Start(Application.StartupPath + "\\ParamTest1.exe", "Test");
    }

    private void button3_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Process.Start(Application.StartupPath + "\\Tool.exe","UserName Password");
    }

The Code of tool.exe:

static class Program
{
    /// <summary>
    /// Der Haupteinstiegspunkt für die Anwendung.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new FormMain(args));
    }
}

And the FormMain:

public partial class FormMain: Form
{
    string Raptoruser = "";

    public FormMain(string[] args)
    {
        InitializeComponent();

...

Visual Studio copies the EXE as planned in the debug Folder.

You need to pass an arguments using ProcessStartInfo.Arguments property. And notice, that if your args contains the spaces you need to add quotes around your args, so, I suggest you need to correct your code smth like this:

Process.Start(new ProcessStartInfo(Application.StartupPath + "\\Tool.exe")
{
    Arguments = String.Format(@"""{0} {1}""", UserName, Password)
}
);

AND also make sure your Tool.exe is placed in the same location with the executable file of the solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM