我正在尝试从C#运行命令行脚本。我希望它在没有外壳的情况下运行并将输出放入我的字符串输出中。它不喜欢p.StartInfo行。我究竟做错了什么?我没有运行p.StartInfo.FileName =“ YOURBATCHFILE.bat”之类的文件,如How To:在C#中执行命令行,获取STD OUT结果。我需要设置“ 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();
Run Code Online (Sandbox Code Playgroud)