【问题标题】:Execute a exe file with arguments using cmd in winforms在winforms中使用cmd执行带参数的exe文件
【发布时间】:2012-05-16 10:53:36
【问题描述】:

我正在开发一个windows应用程序。我正在调用命令提示符,我需要调用exe文件,该exe文件带有参数。

我可以打开命令提示符但不能发送参数

        string  strCmdText = "create-keyfile.exe ..\\KatanaFirmware\\key-template.txt ..\\Keyfiles\\new-keyfile.txt " + cableTextBox.Text.Trim()+" " + startIdTextBox.Text.Trim();
        System.Diagnostics.Process.Start("CMD.exe", strCmdText);

请帮帮我。

谢谢

惩罚

【问题讨论】:

    标签: c# winforms


    【解决方案1】:
     System.Diagnostics.ProcessStartInfo startInfo = new ProcessStartInfo("create-keyfile.exe");
    startInfo.Arguments = "..\\KatanaFirmware\\key-template.txt ..\\Keyfiles\\new-keyfile.txt " + cableTextBox.Text.Trim()+" " + startIdTextBox.Text.Trim();
    System.Diagnostics.Process.Start(startInfo);
    

    你也可以在 Process.Start 上看到MSDN site,这里有关于如何执行 .exe 并将参数传递给它的示例。

    【讨论】:

    • 如果它解决了您的问题,您应该接受答案,以便其他人可以看到该问题已关闭。
    【解决方案2】:
    ProcessStartInfo process = new ProcessStartInfo();
    process.FileName = "yourprogram.exe";
    process.Arguments = strCmdText; // or put your arguments here rather than the string
    Process.Start(process);
    

    【讨论】:

      【解决方案3】:

      你试过了吗

      System.Diagnostics.Process.Start("CMD.exe "+strCmdText);
      

      实际上进一步检查我认为您不需要调用 CMD.EXE 你应该调用你的 exe 文件,除非你当然使用 CMD 来显示一些东西

      string  strCmdText =  "..\\KatanaFirmware\\key-template.txt ..\\Keyfiles\\new-keyfile.txt " + cableTextBox.Text.Trim()+" " + startIdTextBox.Text.Trim();
      System.Diagnostics.Process.Start("create-keyfile.exe", strCmdText);
      

      【讨论】:

        【解决方案4】:

        你试过用/k or /c option cmd

        来自link

        /c : 执行字符串指定的命令,然后停止。

        /k : 执行字符串指定的命令并继续。

           string  strCmdText = "/k create-keyfile.exe ..\\KatanaFirmware\\key-template.txt ..\\Keyfiles\\new-keyfile.txt " + cableTextBox.Text.Trim()+" " + startIdTextBox.Text.Trim();
           System.Diagnostics.Process.Start("CMD.exe", strCmdText);
        

        【讨论】:

          【解决方案5】:

          试试这个。

          string  strCmdText = "KatanaFirmware\\key-template.txt "+ "\\Keyfiles\\new-keyfile.txt " + cableTextBox.Text.Trim()+" " +     startIdTextBox.Text.Trim();
          Process mp = new Process();
          mp.StartInfo.FileName = "E:\\create-keyfile.exe"; //path of the exe that you want to run
          mp.StartInfo.UseShellExecute = false;
          mp.StartInfo.CreateNoWindow = true;
          mp.StartInfo.RedirectStandardInput = true;
          mp.StartInfo.RedirectStandardOutput = true;
          mp.StartInfo.Arguments = strCmdText;
          mp.Start();
          mp.WaitForExit();
          

          希望对你有帮助。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-07-26
            • 2013-05-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-04-18
            相关资源
            最近更新 更多