Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何重定向cmnd输出_C#_Process - Fatal编程技术网

C# 如何重定向cmnd输出

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

我想启动一个cmd文件并将输出重定向到控制台,但它不起作用。我该怎么办?我已经设置了
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输出。