2012-08-16 17 views
9

चलाते समय प्रक्रिया आउटपुट प्राप्त करें क्या किसी भी तरह की प्रक्रिया के मानक आउटपुट को रीडायरेक्ट करने और इसे होने के रूप में कैप्चर करने के लिए वैसे भी है। प्रक्रिया पूरी होने के बाद मैंने जो कुछ भी देखा है, वह केवल ReadToEnd करता है। मैं आउटपुट प्राप्त करने में सक्षम होना चाहता हूं क्योंकि इसे मुद्रित किया जा रहा है।सी #

संपादित करें:

private void ConvertToMPEG() 
    { 
     // Start the child process. 
     Process p = new Process(); 
     // Redirect the output stream of the child process. 
     p.StartInfo.UseShellExecute = false; 
     p.StartInfo.RedirectStandardOutput = true; 
     //Setup filename and arguments 
     p.StartInfo.Arguments = String.Format("-y -i \"{0}\" -target ntsc-dvd -sameq -s 720x480 \"{1}\"", tempDir + "out.avi", tempDir + "out.mpg"); 
     p.StartInfo.FileName = "ffmpeg.exe"; 
     //Handle data received 
     p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); 
     p.Start(); 
    } 

    void p_OutputDataReceived(object sender, DataReceivedEventArgs e) 
    { 
     Debug.WriteLine(e.Data); 
    } 

उत्तर

13

उपयोग Process.OutputDataReceived प्रक्रिया से घटना, अपनी आवश्यकता का डेटा प्राप्त करने के लिए।

उदाहरण:

var myProc= new Process(); 

...    
myProc.StartInfo.RedirectStandardOutput = true; 
myProc.OutputDataReceived += new DataReceivedEventHandler(MyProcOutputHandler); 

... 

private static void MyProcOutputHandler(object sendingProcess, 
      DataReceivedEventArgs outLine) 
{ 
      // Collect the sort command output. 
    if (!String.IsNullOrEmpty(outLine.Data)) 
    { 
     ....  
    } 
} 
+1

हां, और इसके अतिरिक्त आपको काम करने के लिए 'RedirectStandardOutput' को सही करने की आवश्यकता है। – vcsjones

+0

@vcsjones: बस अतिरिक्त पोस्ट wrinting। – Tigran

+0

उत्तर में [यहां पर] (http://stackoverflow.com/a/3642517/74757)। –

3

तो थोड़ा और अधिक खुदाई के बाद मुझे पता चला कि ffmpeg उत्पादन के लिए stderr उपयोग करता है। आउटपुट प्राप्त करने के लिए मेरा संशोधित कोड यहां दिया गया है।

 Process p = new Process(); 

     p.StartInfo.UseShellExecute = false; 

     p.StartInfo.RedirectStandardOutput = true; 
     p.StartInfo.RedirectStandardError = true; 

     p.StartInfo.Arguments = String.Format("-y -i \"{0}\" -target ntsc-dvd -sameq -s 720x480 \"{1}\"", tempDir + "out.avi", tempDir + "out.mpg"); 
     p.StartInfo.FileName = "ffmpeg.exe"; 

     p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived); 
     p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); 

     p.Start(); 

     p.BeginErrorReadLine(); 
     p.WaitForExit();