2013-09-03 7 views
7

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

क्या यह भी संभव है?

+0

कृपया – Patel

+0

को चलाने के दौरान [सी # प्रक्रिया प्रक्रिया आउटपुट प्राप्त करने के संभावित डुप्लिकेट] (http://stackoverflow.com/questions/11994610/c-sharp-get-process-output- भागते समय) –

उत्तर

15

RedirectStandardOutput का उपयोग करें। MSDN से

नमूना:

// Start the child process. 
Process p = new Process(); 
// Redirect the output stream of the child process. 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.FileName = "Write500Lines.exe"; 
p.Start(); 
// Do not wait for the child process to exit before 
// reading to the end of its redirected stream. 
// p.WaitForExit(); 
// Read the output stream first and then wait. 
string output = p.StandardOutput.ReadToEnd(); 
p.WaitForExit(); 

इसके अलावा ReadToEnd() के लिए एक विकल्प यह है कि बेहतर अपने "उत्पादन को देखने, जबकि प्रक्रिया चल रहा है" की आवश्यकता को पूरा करेगा के लिए OutputDataReceived और BeginOutputReadLine() देखते हैं।

0

आप अपने ग # अनुप्रयोग से एक exe निष्पादित और उत्पादन प्राप्त करने के लिए यह तो आप नीचे दिए गए कोड

System.Diagnostics.Process p = new System.Diagnostics.Process();    

p.StartInfo.FileName = "PATH TO YOUR FILE"; 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.Arguments = metalType + " " + graphHeight + " " + graphWidth; 
p.StartInfo.CreateNoWindow = true; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.RedirectStandardError = true;    

p.EnableRaisingEvents = true; 
p.Start();    
svgText = p.StandardOutput.ReadToEnd(); 

using(StreamReader s = p.StandardError) 
{ 
    string error = s.ReadToEnd(); 
    p.WaitForExit(20000); 
} 
उपयोग कर सकते हैं से चाहते हैं

p.EnableRaisingEvents लिखने के forgete मत करो = true

संबंधित मुद्दे