简体   繁体   English

在 C# 中使用命令行参数启动另一个 EXE

[英]Start another EXE with command line parameters in C#

I want do start an EXE file with Parameters.我想用参数启动一个 EXE 文件。

I tried with Button1 to start a consoleapp (works).我尝试使用 Button1 启动一个控制台应用程序(有效)。 With Button2 i tried to open the same consoleapp with Parameters (works).使用 Button2 我试图用参数(有效)打开同一个控制台应用程序。 With Button3 i tried to open the WindowsFromsApplication with Parameters.使用 Button3,我尝试使用参数打开 WindowsFromsApplication。 The WindowsFromsApplication does not run and casts a WindowsMessage, that it can't be opend (but not in the debugmode). WindowsFromsApplication 不运行并投射 WindowsMessage,它无法打开(但不在调试模式下)。

If I use same WindowsFromsApplication wit a desktop shortcut and parameters it works.如果我使用相同的 WindowsFromsApplication 和桌面快捷方式和参数,它就可以工作。

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: 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:和 FormMain:

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

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

... ...

Visual Studio copies the EXE as planned in the debug Folder. Visual Studio 按计划复制调试文件夹中的 EXE。

You need to pass an arguments using ProcessStartInfo.Arguments property.您需要使用 ProcessStartInfo.Arguments 属性传递参数。 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:请注意,如果您的 args 包含您需要在 args 周围添加引号的空格,那么,我建议您需要像这样更正您的代码:

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.并且还要确保您的 Tool.exe 与解决方案的可执行文件位于同一位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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