0
votes

Good day!

I run some *.exe file at my programm. This exe file-console app. So, i start it like that:

 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();     

This prgramm starts and then during operations make console output. So, how to write this output into file?I cannot modify foreigh app.

Thank you!

EDIT: The target app have KeyPress func(or so on) and when i run process with this target app (and UseShellExecute==false)- KeyPress think that some key pressed and target app stops (when you enter some key- target app will stop.)

So, what can i do?

2
You may want to look at file out put in C# msdn.microsoft.com/en-us/library/8bh11f1k.aspxScott
When you're in console mode, you can redirect output from any command to a file by doing this : c:\>your command > theoutputfile.txt. Could that be an option in your case ?Laurent S.
@Bartdude May be. I shuould run my prog into VS,so may be i can write :proc.StartInfo.Arguments="/someParam1:true >text.txt",_NamePipe1) ?user2545071

2 Answers

1
votes

Check this code :

         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();

Got it form this link

Capturing console output from a .NET application (C#)

0
votes

I hope this will work for you

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");


            }  ``