The code is as follows
P.startinfo = new System.Diagnostics.ProcessStartInfo (path, PWD);
P.start ();
Where path is a bat path!
I want to get the return value after execution to determine if the batch process is wrong during the run?
How do I do it?
The contents of the batch process are as follows:
@echo off
for/f "delims="%%a in (pclist.config) does net use \\%%a\ipc$/delete
for/f "delims="%%a in (pclist.config) does net use \\%%a\ipc$%1/user:administrator
Pause
Define the namespaces used by this sample.using system;using system.text;using system.globalization;using System.IO;us ing system.diagnostics;using system.threading;using system.componentmodel;namespace ProcessAsyncStreamSamples{ Class Processnetstreamredirection {//Define static variables shared by class methods. private static StreamWriter Streamerror =null; private static String Neterrorfile = ""; private static StringBuilder netoutput = null; private static bool Errorredirect = FALSE; private static bool Errorswritten = FALSE; public static void Redirectnetcommandstreams () {String netarguments; Process netprocess; Get the input computer name. Console.WriteLine ("Enter the computer name for the net VIEW command:"); Netarguments = Console.ReadLine (). ToUpper (CultureInfo.InvariantCulture); if (String.IsNullOrEmpty (netarguments)) { The Default to the help command if there are not a input argument. Netarguments = "/?"; }//Check If errors should is redirected to a file. Errorswritten = false; Console.WriteLine ("Enter a fully qualified path to an error log file"); Console.WriteLine ("Or just press Enter to write errors to console:"); Neterrorfile = Console.ReadLine (). ToUpper (CultureInfo.InvariantCulture); if (! String.IsNullOrEmpty (Neterrorfile)) {Errorredirect = true; }//Note that at the this point, netarguments and Neterrorfile//is set with user input. If the user did not specify//A error file, then Errorredirect is set to false. Initialize the process and its startinfo properties. netprocess = new Process (); NetProcess.StartInfo.FileName = "Net.exe"; Build the net command argument list. NetProcess.StartInfo.Arguments = String.Format ("View {0}", netarguments); Set UseShellExecute to False for redirection. NetProcess.StartInfo.UseShellExecute = false; Redirect the standard output of the net command. This stream is the read asynchronously using an event handler. NetProcess.StartInfo.RedirectStandardOutput = true; netprocess.outputdatareceived + = new Datareceivedeventhandler (Netoutputdatahandler); Netoutput = new StringBuilder (); if (errorredirect) {//Redirect the error output of the net command. NetProcess.StartInfo.RedirectStandardError = true; netprocess.errordatareceived + = new Datareceivedeventhandler (Neterrordatahandler); } else {//Do not redirect the error output. NetProcess.StartInfo.RedirectStandardError = false; } Console.WriteLine ("\nstarting process:net {0}", netProcess.StartInfo.Arguments); if (Errorredirect) {Console.WriteLine ("Errors'll be written to the file {0}", Neterrorfile); }//Start the process. Netprocess.start (); Start the asynchronous read of the standard output stream. Netprocess.beginoutputreadline (); if (errorredirect) {//Start the asynchronous read of the standard//error stre Am. Netprocess.beginerrorreadline (); }//Let the net command run, collecting the output. Netprocess.waitforexit (); if (streamerror! = null) {//Close the error file. Streamerror.close (); } else {//Set Errorswritten to False if the stream was not Open. Either there is no errors, or the error//file could not being opened. Errorswritten = false; } if (Netoutput.length > 0) {//If the process wrote more than just White space, write the output to the console. Console.WriteLine ("\npublic network shares from net view:\n{0}\n", netoutput); } if (Errorswritten) {//Signal that the error file had something// Written to it. String [] erroroutput = File.ReadAllLines (neterrorfile); if (Erroroutput.length > 0) {Console.WriteLine ("\nthe following error output was app Ended to {0}. ", Neterrorfile); foreach (String errline in Erroroutput) {Console.WriteLine ("{0}", Errline); }} Console.WriteLine (); } netprocess.close (); } private static void Netoutputdatahandler (Object sendingprocess, Datareceivedeventargs outLine) {//Collect The net view command output. if (! String.IsNullOrEmpty (Outline.data)) {//Add the text to the collected output. Netoutput.append (Environment.NewLine + "" + outline.data); }} private static void Neterrordatahandler (Object sendingprocess, Datareceivedeventargs errline ) {//write the error text to the file if there are something//to write and an error file H As been specified. if (! String.IsNullOrEmpty (Errline.data)) {if (!errorswritten) {if (Streamerror = = null) {//Open the file. try {streamerror = new StreamWriter (Neterrorfile, true); } catch (Exception e) {CONSOLE.WR Iteline ("Could not open error file!"); Console.WriteLine (E.message.tostring ()); }} if (Streamerror! = null) {//Write a Header to the file if this is the first//call to the error output handler. Streamerror.writeline (); Streamerror.writeline (DateTime.Now.ToString ()); Streamerror.writeline ("Net View Error Output:"); } Errorswritten = true; } if (Streamerror! = null) {//Write redirected errors to the file.Streamerror.writeline (Errline.data); Streamerror.flush (); } } } }}
String arg = string. Format (backupfmt, server, user, PWD, database, file); Process P = new process (); p.StartInfo.FileName = "cmd.exe"; P.startinfo.useshellexecute = false; P.startinfo.workingdirectory = location + @ "Bin"; p.startinfo.arguments = arg; P.startinfo.windowstyle = Processwindowstyle.hidden; P.startinfo.createnowindow = true; P.startinfo.redirectstandardinput = true; P.startinfo.redirectstandardoutput = true; P.start (); StreamWriter SW = p.standardinput; Sw. WriteLine (ARG); Sw. WriteLine (Environment.NewLine); Sw. Close (); #if DEBUG StreamReader sr = p.standardoutput;string str = Sr. ReadToEnd (); if (!string. IsNullOrEmpty (str)) {Console.WriteLine ("Restore File:" + file); Console.WriteLine ("Message:" + str);} Sr. Close (); #endif p.waitforexit (); P.close ();
Run a DOS command in C #, and intercept the output, output stream problems, this problem I have implemented in Java, because in C # has not encountered a similar situation, in order to avoid every time others asked to repeat the case of the presentation, specifically to do a simple example, Implement ping a Web site in WinForm, and display the results of the ping in a text box.
private void Btnexecute_click (object sender, EventArgs e) {Tbresult. Text = ""; ProcessStartInfo start = new ProcessStartInfo ("Ping.exe");//Set Run command line file to ask Ping.exe file, this filesystem will find itself//if it is another EXE file, it is possible You need to specify a detailed path, such as running WinRar.exe start. Arguments = txtcommand.text;//Set command parameter start. CreateNoWindow = true;//does not display the DOS command line window start. Redirectstandardoutput = true;//start. Redirectstandardinput = true;//start. UseShellExecute = false;//Whether the operating system shell process initiator is specified (start); StreamReader reader = p.standardoutput;//intercepts the output stream string line = reader. ReadLine ();//reads one row at a time (!reader. Endofstream) {Tbresult.appendtext (line+ ""); line = reader. ReadLine (); } p.waitforexit ();//wait for the program to finish executing the exit process p.close ();//close Process reader. Close ();//Closed stream}
C#process How do I get the return value after executing a batch?