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

标签 c# cmd keystore

我正在尝试使用 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# - 在 C# 中使用参数执行命令行 .exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37549227/

相关文章:

java - Tomcat 7 和无效的 keystore 格式

java - 构建证书颁发机构架构

java - 为什么我无法以这种方式关闭我的 JAR 可执行文件?

python - 激活虚拟环境并以批处理文件方式启动 jupyter notebook

regex - 在批处理脚本中打印第二个标记之后的所有标记

java - 如何在不转换为 keystore 的情况下从 PEM 证书和 key 构建 SSLSocketFactory?

c# - 将数据从 Azure Blob 流式传输回客户端

c# - .net 中类和结构之间的实际区别(不是概念上的)?

c# - 在运行时更改其他项目的 app.config 值

c# - MVVM WPF Datagrid TemplateColumn 组合框所选项目不起作用