2

I tried 3-4 versions and none worked for me.. I have a small exe for which i dont have the source anymore but it works good, but i still need the result string from it,

Process proc = new Process();
ProcessStartInfo StartInfo = new ProcessStartInfo();
StartInfo.RedirectStandardError = true;
StartInfo.RedirectStandardOutput = true;
StartInfo.FileName = path + "calculate.exe";
StartInfo.Arguments = input;
StartInfo.UseShellExecute = false;
StartInfo.CreateNoWindow = true;

proc.StartInfo = StartInfo;

proc.Start();

proc.WaitForExit();

string output = proc.StandardOutput.ReadToEnd();

MessageBox.Show(output);

i get an empty MessageBox, any ideas why? When i add a breakpoint at WaitForExit, StandardOutput says that it isn't redirected or the process didn't start yet.

4

2 回答 2

4

你需要设置RedirectStandardOutput,而不是RedirectStandardError
(另外,确保该过程真正写入标准输出而不是标准错误)

于 2013-07-23T17:29:08.560 回答
0

你试过这个吗?

// This is the code for the base process
        Process myProcess = new Process();
        // Start a new instance of this program but specify the 'spawned' version.
        ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(args[0], "spawn");
        myProcessStartInfo.UseShellExecute = false;
        myProcessStartInfo.RedirectStandardOutput = true;
        myProcess.StartInfo = myProcessStartInfo;
        myProcess.Start();
        StreamReader myStreamReader = myProcess.StandardOutput;
        // Read the standard output of the spawned process. 
        string myString = myStreamReader.ReadLine();
        Console.WriteLine(myString);

        myProcess.WaitForExit();
        myProcess.Close();

资料来源:MSDN

于 2013-07-23T17:38:10.817 回答