簡體   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