简体   繁体   中英

Cannot get the output of command line in c#

I want to get the output of an execution in c# and I referred to this question . But I only have the output be printed on the console but not stored in the specified string. Here is my code: `

        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        //p.StartInfo.CreateNoWindow = true;

        p.StartInfo.FileName = "ffmpeg.exe";
        p.StartInfo.Arguments = " -i 1.flv";
        p.Start();


        p.WaitForExit();
        string output = p.StandardOutput.ReadToEnd();
        Console.WriteLine(output);
        Console.ReadLine();`

The output string is still empty after the execution of these codes. Plus if I keep the line p.StartInfo.CreateNoWindow = true; , no words will be printed on the console at all, why this happen? I thought the line will only stop a new window being created.

Move string output = p.StandardOutput.ReadToEnd(); inside wait for exit. How are you going to read data when it already exit.

    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    //p.StartInfo.CreateNoWindow = true;

    p.StartInfo.FileName = "ffmpeg.exe";
    p.StartInfo.Arguments = " -i 1.flv";
    p.Start();
    string output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();

    Console.WriteLine(output);
    Console.ReadLine();`

I'd try the following:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;

p.StartInfo.FileName = "ffmpeg.exe";
p.StartInfo.Arguments = " -i 1.flv";
p.Start();

while (!p.HasExited)
{
   string output = p.StandardOutput.ReadToEnd();
}

I also suggest you have a look at the BeginReadOutputLine method in this example given in the MS documentation . Being asynchronous, it will be called even though you use WaitForExit .

A boiled down version of this is:

// Start the asynchronous read of the output stream.
p.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
p.EnableRaisingEvents = true;
p.BeginOutputReadLine();
p.Start();
p.WaitForExit();
p.Close();

private static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
    // Collect the command output. 
    if (!String.IsNullOrEmpty(outLine.Data))
    {
        numOutputLines++;

        // Add the text to the output
        Console.WriteLine(Environment.NewLine + 
                "[" + numOutputLines.ToString() + "] - " + outLine.Data);
    }
}

How about switch those two line?

p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM