C# 如何将我的程序的控制台输出重定向到.Net文件中

C# 如何将我的程序的控制台输出重定向到.Net文件中,c#,redirect,process,output,C#,Redirect,Process,Output,你好 我在程序中运行一些*.exe文件。这是一个exe文件控制台应用程序。 所以,我是这样开始的: proc = new Process(); proc.StartInfo.UseShellExecute = true; proc.StartInfo.RedirectStandardOutput = false; proc.StartInfo.RedirectStandardInput = false; proc.StartInfo.FileName =

你好

我在程序中运行一些*.exe文件。这是一个exe文件控制台应用程序。 所以,我是这样开始的:

 proc = new Process();               
 proc.StartInfo.UseShellExecute = true;
 proc.StartInfo.RedirectStandardOutput = false;
 proc.StartInfo.RedirectStandardInput = false;
 proc.StartInfo.FileName = procpath;
 proc.StartInfo.Arguments = String.Format("/someParam1:true , _NamePipe1);                                  
 proc.Start();     
此PRGRAM启动,然后在操作期间生成控制台输出。 那么,如何将此输出写入文件?我无法修改foreigh应用程序

谢谢大家!

编辑:目标应用程序具有按键功能(或诸如此类),当我使用此目标应用程序运行进程(并使用ShellExecute==false)时,按键会认为某个按键被按下,目标应用程序停止(当您输入某个按键时,目标应用程序将停止)

那么,我能做什么呢?

检查以下代码:

         Process proc = new Process();               
         proc.StartInfo.UseShellExecute = true;
         proc.StartInfo.RedirectStandardOutput = false;
         proc.StartInfo.RedirectStandardInput = false;
         proc.StartInfo.FileName = "procpath";
         proc.StartInfo.Arguments = String.Format("/someParam1:true , _NamePipe1");                                  
         proc.Start();

         StreamWriter writer = File.CreateText("newfile.txt");

         writer.WriteLine(proc.StandardOutput.ReadToEnd());

         proc.WaitForExit();

         writer.Flush();
         writer.Close();
从这个链接上找到的


我希望这对你有用

private void reg(string host)
            {

                string build = "QUERY \\\\" + host + "\\HKEY_USERS";
                string parms = @build;
                string output = "";
                string error = string.Empty;

                ProcessStartInfo psi = new ProcessStartInfo("reg.exe", parms);

                psi.RedirectStandardOutput = true;
                psi.RedirectStandardError = true;
                psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                psi.UseShellExecute = false;
                System.Diagnostics.Process reg;
                reg = System.Diagnostics.Process.Start(psi);
                using (System.IO.StreamReader myOutput = reg.StandardOutput)
                {
                    output = myOutput.ReadToEnd();
                }
                using (System.IO.StreamReader myError = reg.StandardError)
                {
                    error = myError.ReadToEnd();

                }
                Output.AppendText(output + "\n");


            }  ``

您可能希望查看C中的文件输出。当您处于控制台模式时,您可以通过执行以下操作将任何命令的输出重定向到文件:
C:\>your command>output file.txt
。这可能是你的选择吗?巴特杜德可能是。我应该将我的程序运行到VS中,所以可能我可以写:proc.StartInfo.Arguments=“/someParam1:true>text.txt”,_NamePipe1)?