繁体   English   中英

C#中带参数执行Command line.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);
}

当涉及到从 C# 执行 CLI 进程时,这似乎是一项简单的任务,但有很多陷阱您可能直到很久以后才注意到。 例如,无论是目前给出的答案也不会工作,如果子进程足够的数据写入stdout,如解释在这里

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

您的代码将如下所示:

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

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

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

以下是运行时方法参数的值。 对于需要正确设置对象 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;
  }

在德米特里的帮助和以下资源之间,

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

我能够拼凑起来。 谢谢!

尝试这个!

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

暂无
暂无

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

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