簡體   English   中英

在 C# 中使用命令行參數啟動另一個 EXE

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

我想用參數啟動一個 EXE 文件。

我嘗試使用 Button1 啟動一個控制台應用程序(有效)。 使用 Button2 我試圖用參數(有效)打開同一個控制台應用程序。 使用 Button3,我嘗試使用參數打開 WindowsFromsApplication。 WindowsFromsApplication 不運行並投射 WindowsMessage,它無法打開(但不在調試模式下)。

如果我使用相同的 WindowsFromsApplication 和桌面快捷方式和參數,它就可以工作。

我的解決方案有什么問題?

 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");
    }

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));
    }
}

和 FormMain:

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

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

...

Visual Studio 按計划復制調試文件夾中的 EXE。

您需要使用 ProcessStartInfo.Arguments 屬性傳遞參數。 請注意,如果您的 args 包含您需要在 args 周圍添加引號的空格,那么,我建議您需要像這樣更正您的代碼:

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

並且還要確保您的 Tool.exe 與解決方案的可執行文件位於同一位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM