2011-05-14 7 views
5

कैसे आप सी # में एक जीयूआई प्रपत्र के भीतर एक बैच स्क्रिप्ट को निष्पादित करेंगे भीतर एक बैच फ़ाइल को चलाने के लिएकैसे एक सी # जीयूआई रूप

किसी को भी एक नमूना प्रदान कर सके कृपया?

उत्तर

4

यह उदाहरण दो टेक्स्ट बॉक्स (RunResults और Errors) के साथ एक विंडोज फॉर्म एप्लिकेशन मानता है।

// Remember to also add a using System.Diagnostics at the top of the class 
private void RunIt_Click(object sender, EventArgs e) 
{ 
    using (Process p = new Process()) 
    { 
     p.StartInfo.WorkingDirectory = "<path to batch file folder>"; 
     p.StartInfo.FileName = "<path to batch file itself>"; 
     p.StartInfo.UseShellExecute = false; 
     p.StartInfo.RedirectStandardOutput = true; 
     p.StartInfo.RedirectStandardError = true; 
     p.Start(); 
     p.WaitForExit(); 

     // Capture output from batch file written to stdout and put in the 
     // RunResults textbox 
     string output = p.StandardOutput.ReadToEnd(); 
     if (!String.IsNullOrEmpty(output) && output.Trim() != "") 
     { 
      this.RunResults.Text = output; 
     } 

     // Capture any errors written to stderr and put in the errors textbox. 
     string errors = p.StandardError.ReadToEnd(); 
     if (!String.IsNullOrEmpty(errors) & errors.Trim() != "")) 
     { 
      this.Errors.Text = errors; 
     } 
    } 
} 

अपडेट किया गया:

नमूना ऊपर एक बटन RunIt कहा जाता है के लिए एक बटन क्लिक करें घटना है। फॉर्म, RunResults और Errors पर कुछ टेक्स्ट बॉक्स हैं जहां हम stdout और stderr के परिणाम लिखते हैं।

+0

@ केव हाय, धन्यवाद, उदाहरण के लिए एक टेक्स्ट बॉक्स में इसे कैसे शुरू किया जाएगा? – Mike

+0

@ माइक - मैंने अपना जवाब अपडेट कर लिया है, क्या आपका मतलब है? – Kev

+0

@ केव, हाय, हाँ, धन्यवाद, मैंने इसे लिखा है, हालांकि मुझे कुछ संकलन त्रुटियां मिलती हैं, मुझे लगता है कि ऐसा इसलिए हो सकता है क्योंकि मैं नेट फ्रेमवर्क 3.5/वीएस 2008 (अपग्रेड करने का प्रयास करूंगा ..), – Mike

5

System.Diagnotics.Process.Start ("yourbatch.bat"); इसे करना चाहिए

Another thread covering the same issue

+0

@Will ए हाय, तत्काल उत्तर के लिए धन्यवाद, हालांकि आप इसे एक रूप में कैसे कार्यान्वित करेंगे? – Mike

+0

@ माइक - बिल्कुल लिखे गए, माइक - या क्या आप वास्तव में बैच फ़ाइल को चलाने के लिए चाहते हैं और आउटपुट को फॉर्म पर UI के भीतर प्रदर्शित किया गया है? –

+0

@ विल ए हां मैं यूआई के भीतर फॉर्म – Mike

1

मैं द्वारा एक जीयूआई फॉर्म के भीतर निष्पादित करने का मतलब है कि आप कुछ UI-Control के भीतर निष्पादन परिणाम दिखा रहे हैं।

हो सकता है कि कुछ इस तरह:

private void runSyncAndGetResults_Click(object sender, System.EventArgs e)  
{ 
    System.Diagnostics.ProcessStartInfo psi = 
     new System.Diagnostics.ProcessStartInfo(@"C:\batch.bat"); 

    psi.RedirectStandardOutput = true; 
    psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
    psi.UseShellExecute = false; 

    System.Diagnostics.Process batchProcess; 
    batchProcess = System.Diagnostics.Process.Start(psi); 

    System.IO.StreamReader myOutput = batchProcess.StandardOutput; 
    batchProcess.WaitForExit(2000); 
    if (batchProcess.HasExited) 
    { 
     string output = myOutput.ReadToEnd(); 

     // Print 'output' string to UI-control 
    } 
} 

उदाहरण here से लिया।

+0

उदाहरण के लिए आप इस फ़ंक्शन पर कॉल कैसे करेंगे? – Mike

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