我正在尝试使用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"
Run Code Online (Sandbox Code Playgroud)
问题可能是我如何调用和格式化我在该变量命令中使用的字符串.
关于可能出现什么问题的任何想法?
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);
Run Code Online (Sandbox Code Playgroud)
一旦完成,我就不会在变量结果中找回任何信息或错误.
Dmi*_*nko 13
等待进程结束(让它完成它的工作):
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13758 次 |
| 最近记录: |