Running exe with parameters doesn't work
Asked Answered
M

4

5
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.

Moan answered 19/2, 2015 at 13:13 Comment(4)
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.Teem
@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?Moan
UseShellExecute = false ?Criterion
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.Teem
T
14

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();
Teem answered 19/2, 2015 at 13:17 Comment(4)
Well, "doesn't work" is not a valid error description.Teem
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 startedMoan
Sorry, fixing the code... See if compiles now. Got mixed up with Process and ProcessStartInfo instances.Teem
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 somethingMoan
C
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.

Criterion answered 19/2, 2015 at 13:19 Comment(0)
C
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();
Cowes answered 19/11, 2021 at 7:45 Comment(0)
M
-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;

}
Mantua answered 10/8, 2019 at 15:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.