2024年1月22日发(作者:)
// Build the net command argument list. nts = ("view {0}",
netArguments); // Set UseShellExecute to false for redirection. llExecute = false; // Redirect the standard output of the net command.
// This stream is read asynchronously using an event handler. ctStandardOutput = true; DataReceived += new DataReceivedEventHandler(NetOutputDataHandler); netOutput = new StringBuilder(); if (errorRedirect) { // Redirect the error output of the net command.
ctStandardError = true; ataReceived += new DataReceivedEventHandler(NetErrorDataHandler); } else
{ // Do not redirect the error output. ctStandardError = false; } ine("/nStarting process: net {0}",
nts); if (errorRedirect) { ine("Errors will be written to the file {0}",
netErrorFile); } // Start the process. (); // Start the asynchronous read of the standard output stream. utputReadLine(); if (errorRedirect) { // Start the asynchronous read of the standard // error stream. rrorReadLine(); } // Let the net command run, collecting the output. rExit(); if (streamError != null) { // Close the error file. (); }
else
{ // Set errorsWritten to false if the stream is not // open. Either there are no errors, or the error // file could not be opened. errorsWritten = false; } if ( > 0) { // If the process wrote more than just // white space, write the output to the console. ine("/nPublic network shares from net view:/n{0}/n",
netOutput); } if (errorsWritten) { // Signal that the error file had something
// written to it. String [] errorOutput = lLines(netErrorFile); if ( > 0) { ine("/nThe following error output was appended to {0}.", netErrorFile); foreach (String errLine in errorOutput) { ine(" {0}", errLine); } } ine(); } (); } private static void NetOutputDataHandler(object sendingProcess,
DataReceivedEventArgs outLine) { // Collect the net view command output. if (!OrEmpty()) { // Add the text to the collected output. (e + " " + ); } } private static void NetErrorDataHandler(object sendingProcess,
DataReceivedEventArgs errLine) { // Write the error text to the file if there is something // to write and an error file has been specified. if (!OrEmpty())
{ if (!errorsWritten) { if (streamError == null) { // Open the file. try
{ streamError = new StreamWriter(netErrorFile, true); } catch (Exception e) { ine("Could not open error file!"); ine(ng()); } } if (streamError != null) { // Write a header to the file if this is the first // call to the error output handler. ine(); ine(ng()); ine("Net View error output:"); } errorsWritten = true; } if (streamError != null) { // Write redirected errors to the file. ine(); (); } } } }}


发布评论