C# 如何重定向cmnd输出
我想启动一个cmd文件并将输出重定向到控制台,但它不起作用。我该怎么办?我已经设置了C# 如何重定向cmnd输出,c#,process,C#,Process,我想启动一个cmd文件并将输出重定向到控制台,但它不起作用。我该怎么办?我已经设置了startInfo.RedirectStandardOutput=true com = "Parameter"; System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.Pro
startInfo.RedirectStandardOutput=true
com = "Parameter";
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo =
new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = Properties.Settings.Default.pathTo+ @"\Make\copy.cmd";
startInfo.Arguments = com;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
output = process.StandardOutput;
string ln;
while ((ln = output.ReadLine()) != null)
{
Console.WriteLine(line);
}
process.WaitForExit();
从
设置startInfo.UseShellExecute=false代码>
然后您可以得到如下输出:
Console.WriteLine(process.StandardOutput.ReadToEnd());
从
设置startInfo.UseShellExecute=false代码>
然后您可以得到如下输出:
Console.WriteLine(process.StandardOutput.ReadToEnd());
现在,我有了这个解决方案:
com = "Parameter";;
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = Properties.Settings.Default.pathTo + @"\Make\copy.cmd";
startInfo.Arguments = com;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
Console.WriteLine(process.StandardOutput.ReadToEnd());
Console.WriteLine(process.StandardError.ReadToEnd());
现在,我有了这个解决方案:
com = "Parameter";;
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = Properties.Settings.Default.pathTo + @"\Make\copy.cmd";
startInfo.Arguments = com;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
Console.WriteLine(process.StandardOutput.ReadToEnd());
Console.WriteLine(process.StandardError.ReadToEnd());
可能的重复和可能的重复,但它不起作用。我犯了一个错误。不能将startInfo.UseShellExecute设置为true;和startInfo.RedirectStandardOutput=true;抱歉-我的坏ShellExecute应该设置为false以重定向STD输出。它不起作用。我犯了一个错误。不能将startInfo.UseShellExecute设置为true;和startInfo.RedirectStandardOutput=true;抱歉-my bad-ShellExecute应设置为false以重定向STD输出。