कैसे आप सी # में एक जीयूआई प्रपत्र के भीतर एक बैच स्क्रिप्ट को निष्पादित करेंगे भीतर एक बैच फ़ाइल को चलाने के लिएकैसे एक सी # जीयूआई रूप
किसी को भी एक नमूना प्रदान कर सके कृपया?
कैसे आप सी # में एक जीयूआई प्रपत्र के भीतर एक बैच स्क्रिप्ट को निष्पादित करेंगे भीतर एक बैच फ़ाइल को चलाने के लिएकैसे एक सी # जीयूआई रूप
किसी को भी एक नमूना प्रदान कर सके कृपया?
यह उदाहरण दो टेक्स्ट बॉक्स (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
के परिणाम लिखते हैं।
System.Diagnotics.Process.Start ("yourbatch.bat"); इसे करना चाहिए
मैं द्वारा एक जीयूआई फॉर्म के भीतर निष्पादित करने का मतलब है कि आप कुछ 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 से लिया।
उदाहरण के लिए आप इस फ़ंक्शन पर कॉल कैसे करेंगे? – Mike
@ केव हाय, धन्यवाद, उदाहरण के लिए एक टेक्स्ट बॉक्स में इसे कैसे शुरू किया जाएगा? – Mike
@ माइक - मैंने अपना जवाब अपडेट कर लिया है, क्या आपका मतलब है? – Kev
@ केव, हाय, हाँ, धन्यवाद, मैंने इसे लिखा है, हालांकि मुझे कुछ संकलन त्रुटियां मिलती हैं, मुझे लगता है कि ऐसा इसलिए हो सकता है क्योंकि मैं नेट फ्रेमवर्क 3.5/वीएस 2008 (अपग्रेड करने का प्रयास करूंगा ..), – Mike