4
var p = Process.Start(@"c:\PsTools\PsExec.exe", @"C:\Windows\System32\notepad.exe");
var err = p.StandardError.ReadToEnd();
var msg = p.StandardOutput.ReadToEnd();
lblStatusResponse.Text = "Err: " + err + "Msg: " + msg;

Why is my code not working?

I getting error:

System.InvalidOperationException: StandardError has not been redirected.

But when I add following:

p.StartInfo.RedirectStandardError = true;
var p = Process.Start(@"c:\PsTools\PsExec.exe", @"C:\Windows\System32\notepad.exe");) 

it still gets the same error.

The main problem is that I wanna execute a exe with arguments, but I can't get it to work.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MrProgram
  • 5,044
  • 13
  • 58
  • 98
  • You changed your question so significantly that all answers are rendered invalid. I'm going to revert this. Also, "Why is my code not working" is not a valid question unless you tell us in what way it is not working. – Thorsten Dittmar Feb 19 '15 at 13:23
  • @ThorstenDittmar "The main problem is that I wanna execute a exe with arguments". Isn't that the problem? Itäs not working because exe is not firing? – MrProgram Feb 19 '15 at 13:25
  • UseShellExecute = false ? – Adrian Salazar Feb 19 '15 at 13:25
  • 1
    Please read the SO guidelines as to why "It's not working" is not a valid question. Hint: Do you get errors/exceptions? Are you not getting the expected output? First you say you get an exception, then you change your code *and* your question and then say it just "doesn't work", which leads me to close-vote your question for unclear what you're asking. – Thorsten Dittmar Feb 19 '15 at 13:26

4 Answers4

12

The following code generates a new p, this ignoring the settings you change in the previous instance:

var p = Process.Start(@"c:\PsTools\PsExec.exe", @"C:\Windows\System32\notepad.exe");) 

So it doesn't really matter whether you initialize p like this

p.StartInfo.RedirectStandardError = true;

or not.

What you need to do

You need to create a ProcessStartInfo object, configure it and then pass it to Process.Start.

ProcessStartInfo p = new ProcessStartInfo(@"c:\PsTools\PsExec.exe", @"C:\Windows\System32\notepad.exe");
p.UseShellExecute = false;
p.RedirectStandardError = true;
p.RedirectStandardOutput = true;

Process proc = Process.Start(p);

var err = proc.StandardError.ReadToEnd();
var msg = proc.StandardOutput.ReadToEnd();
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • 1
    Well, "doesn't work" is not a valid error description. – Thorsten Dittmar Feb 19 '15 at 13:24
  • Your code won't compile, p.StandardError/StandardOutput don't exists. I don't know what to say, I have all code inside try/catch and get no error, but I can see in process list that notepad hasn't started – MrProgram Feb 19 '15 at 13:28
  • Sorry, fixing the code... See if compiles now. Got mixed up with `Process` and `ProcessStartInfo` instances. – Thorsten Dittmar Feb 19 '15 at 13:28
  • 2
    Sorry. This actually worked. I Just had to debug my project and try. I was using my IIS before and the changes didn't compile or something – MrProgram Feb 19 '15 at 13:32
1

Taken from MSDN: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.110).aspx

The StandardOutput stream has not been defined for redirection; ensure ProcessStartInfo.RedirectStandardOutput is set to true and ProcessStartInfo.UseShellExecute is set to false.

So remember to set those flags as instructed by MS.

Adrian Salazar
  • 5,279
  • 34
  • 51
0
     Process proc = new Process();
     proc.StartInfo.FileName = "cmd.exe";
     proc.StartInfo.UseShellExecute = false;
     proc.StartInfo.RedirectStandardError = true;
     proc.StartInfo.RedirectStandardOutput = true;
     proc.StartInfo.Arguments = "/C " + command; //Enter your own command
     proc.Start();
     string output =proc.StandardOutput.ReadToEnd();
-1

I know this is not the same code you have but this is was the only working code I have , that will run external command/process in C# , and return all of it output/errors to the application main window

public void Processing()
{
    //Create and start the ffmpeg process
    System.Diagnostics.ProcessStartInfo psi = new ProcessStartInfo("ffmpeg")
    { // this is fully command argument you can make it according to user input 
        Arguments = "-y -i  '/mnt/Disk2/Video/Antina03.jpg' pp.mp4 ",
        RedirectStandardOutput = true,
        WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
        UseShellExecute = false,
        RedirectStandardError=true,
        RedirectStandardInput=true
    };
    System.Diagnostics.Process ischk;
    ischk = System.Diagnostics.Process.Start(psi);
    ischk.WaitForExit();
    ////Create a streamreader to capture the output of ischk
    System.IO.StreamReader ischkout = ischk.StandardOutput;
    ischk.WaitForExit();
    if (ischk.HasExited) // this condition very important to make asynchronous output  
    {
        string output = ischkout.ReadToEnd();
        out0 = output;
    }

    /// in case you got error message 
    System.IO.StreamReader iserror = ischk.StandardError;
    ischk.WaitForExit();
    if (ischk.HasExited)
    {
        string output = iserror.ReadToEnd();
        out0 = output;
    }

}

if you want to run this process just call the function Processing() BTW out0 are global variable so it can use out the function .

credit

I'm using MonoDevlop "C# devloping tool on Linux " and I get the output this way :-

public MainWindow() : base(Gtk.WindowType.Toplevel)
{
    Build();
    Processing();
    textview2.Buffer.Text = out0;

}
Salem
  • 654
  • 7
  • 24