我正在尝试从 C# 运行命令行脚本。我希望它在没有 shell 的情况下运行并将输出放入我的字符串输出中。它不喜欢 p.StartInfo 行。我究竟做错了什么?我没有运行像 p.StartInfo.FileName = "YOURBATCHFILE.bat" 这样的文件如何:在 C# 中执行命令行,获取 STD OUT 结果 https://stackoverflow.com/questions/206323/how-to-execute-command-line-in-c-get-std-out-results。我需要设置“CMD.exe”和命令行字符串。我尝试过 p.Start("CMD.exe", strCmdText);但这给了我错误:“无法使用实例引用访问 Memer 'System.Diagnostics.Process.Start(string,string)';而是使用类型名称来限定它。”
string ipAddress;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
string strCmdText;
strCmdText = "tracert -d " + ipAdress;
p.StartInfo("CMD.exe", strCmdText);
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
这段代码给了我正确的输出。
const string ipAddress = "127.0.0.1";
Process process = new Process
{
StartInfo =
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
FileName = "cmd.exe",
Arguments = "/C tracert -d " + ipAddress
}
};
process.Start();
process.WaitForExit();
if(process.HasExited)
{
string output = process.StandardOutput.ReadToEnd();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)