我的asp.net控制台应用程序中有以下代码: -
var processInfo = new ProcessStartInfo("cmd.exe");
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
var process = Process.Start(processInfo);
process.StandardInput.WriteLine("nslookup");
process.StandardInput.WriteLine("set type=mx");
process.StandardInput.WriteLine(domainname);
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
Console.WriteLine("output>>" + e.Data);
process.BeginOutputReadLine();
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
Console.WriteLine("error>>" + e.Data);
process.BeginErrorReadLine();
process.WaitForExit();
Console.WriteLine("ExitCode: {0}", process.ExitCode);
process.Close();
我试图运行以下命令并获得结果: -
nslookup
set type=mx
domainname
但是当代码到达此行process.StandardInput.WriteLine("nslookup");
时,我得到了此异常StandardIn has not been redirected
。
答案 0 :(得分:1)
错误非常具有描述性:StandardIn has not been redirected
您已将行中的stderr和stdout重定向:
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
但是,由于错误显示您未重定向StandardIn
您可以通过以下方式重定向stdin来实现:
processInfo.RedirectStandardInput = true;