0

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?

user2545071
  • 1,408
  • 2
  • 24
  • 46
  • You may want to look at file out put in C# http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx – Scott Apr 09 '14 at 07:12
  • 1
    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. Apr 09 '14 at 07:14
  • @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 Apr 09 '14 at 07:35

2 Answers2

0

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#)

Community
  • 1
  • 1
yazan
  • 600
  • 7
  • 12
  • when you run the code , we get : 'proc.StandardOutput' threw an exception of type 'System.InvalidOperationException' – faheem khan Jul 26 '23 at 14:13
0

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


            }  ``
HackerMan
  • 904
  • 7
  • 11