在 C# 中使用参数执行命令行 .exe

Executing Command line .exe with parameters in C#

我正在尝试使用 C# 中的参数执行命令行程序。我原以为在 C# 中坚持并实现这一目标是微不足道的,但事实证明,即使使用本网站及其他网站上的所有可用资源,它也具有挑战性。我不知所措,所以我会提供尽可能详细的信息。

我当前的方法和代码如下,在调试器中变量命令具有以下值。

command = "C:\Folder1\Interfaces\Folder2\Common\JREbin\keytool.exe -import -noprompt -trustcacerts -alias myserver.us.goodstuff.world -file C:\SSL_CERT.cer -storepass changeit -keystore keystore.jks"

问题可能出在我如何调用和格式化我在该变量命令中使用的字符串。

对可能出现的问题有什么想法吗?

ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);

    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    procStartInfo.CreateNoWindow = true;
    Process process = new Process();
    process.StartInfo = procStartInfo;
    process.Start();
    string result = process.StandardOutput.ReadToEnd();
    Console.WriteLine(result);

我在变量结果完成后没有返回任何信息或错误。

等待进程结束(让它完成它的工作):

ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);

procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;

// wrap IDisposable into using (in order to release hProcess) 
using(Process process = new Process()) {
  process.StartInfo = procStartInfo;
  process.Start();

  // Add this: wait until process does its work
  process.WaitForExit();

  // and only then read the result
  string result = process.StandardOutput.ReadToEnd();
  Console.WriteLine(result);
}

我意识到我可能遗漏了一些细节,有些人将来可能需要解决这个问题。

这里是 运行 时方法参数的值。我对 ProcessStartInfo 和 Process 对象需要正确设置的内容有些困惑,我想其他人也可能会这样。

exeDir = "C:\folder1\folder2\bin\keytool.exe"

args = "-delete -noprompt -alias server.us.goodstuff.world -storepass changeit -keystore keystore.jks"

public bool ExecuteCommand(string exeDir, string args)
{
  try
  {
    ProcessStartInfo procStartInfo = new ProcessStartInfo();

    procStartInfo.FileName = exeDir;
    procStartInfo.Arguments = args;
    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    procStartInfo.CreateNoWindow = true;

    using (Process process = new Process())
    {
      process.StartInfo = procStartInfo;
      process.Start();

      process.WaitForExit();

      string result = process.StandardOutput.ReadToEnd();
      Console.WriteLine(result);
    }
    return true;
  }
  catch (Exception ex)
  {
    Console.WriteLine("*** Error occured executing the following commands.");
    Console.WriteLine(exeDir);
    Console.WriteLine(args);
    Console.WriteLine(ex.Message);
    return false;
  }

在 Dmitry 的协助和以下资源之间,

http://www.codeproject.com/Articles/25983/How-to-Execute-a-Command-in-C

我能够把它拼凑起来。谢谢!

当谈到从 C# 执行 CLI 进程时,这似乎是一项简单的任务,但其中有很多您可能直到很久以后才注意到的陷阱。例如,如果子进程将足够的数据写入标准输出,则当前给出的两个答案都将不起作用,如 here.

所述

我编写了一个库,通过完全抽象 Process 交互来简化 CLI 的使用,通过执行一个方法解决整个任务 - CliWrap.

您的代码将如下所示:

var cmd = Cli.Wrap("cmd")
    .WithArguments(a => a.Add("/c").Add(command));

var result = await cmd.ExecuteBufferedAsync();
var stdOut = result.StandardOutput;

试试这个!

Console.WriteLine("Creating Directories...");
Directory.CreateDirectory("Old_Files");
Directory.CreateDirectory("Old_Files\529930");
Directory.CreateDirectory("Old_Files\530610");
Directory.CreateDirectory("Old_Files\530611");
Directory.CreateDirectory("Old_Files\564190");
Console.WriteLine("Working On The First File...");
File.Copy("529930\re_dlc_000.pak", "Old_Files\529930");
var process = new ProcessStartInfo();
process.FileName = "RE7 - Patcher.exe";
process.Arguments = "-v -d -s '529930\re_dlc_000.pak' '529930.REFA' 're_dlc_000.pak'";
File.Move("re_dlc_000.pak", "529930");
Console.WriteLine("First One Completed!");