001 | //Definethenamespacesusedbythissample. |
004 | using System.Globalization; |
006 | using System.Diagnostics; |
007 | using System.Threading; |
008 | using System.ComponentModel; |
011 | namespace ProcessAsyncStreamSamples |
014 | class ProcessNetStreamRedirection |
016 | //Definestaticvariablessharedbyclassmethods. |
017 | private static StreamWriterstreamError= null ; |
018 | private static StringnetErrorFile= "" ; |
019 | private static StringBuildernetOutput= null ; |
020 | private static bool errorRedirect= false ; |
021 | private static bool errorsWritten= false ; |
023 | public static void RedirectNetCommandStreams() |
028 | //Gettheinputcomputername. |
029 | Console.WriteLine( "Enterthecomputernameforthenetviewcommand:" ); |
030 | netArguments=Console.ReadLine().ToUpper(CultureInfo.InvariantCulture); |
031 | if (String.IsNullOrEmpty(netArguments)) |
033 | //Defaulttothehelpcommandifthereisnotaninputargument. |
037 | //Checkiferrorsshouldberedirectedtoafile. |
039 | Console.WriteLine( "Enterafullyqualifiedpathtoanerrorlogfile" ); |
040 | Console.WriteLine( "orjustpressEntertowriteerrorstoconsole:" ); |
041 | netErrorFile=Console.ReadLine().ToUpper(CultureInfo.InvariantCulture); |
042 | if (!String.IsNullOrEmpty(netErrorFile)) |
047 | //Notethatatthispoint,netArgumentsandnetErrorFile |
048 | //aresetwithuserinput.Iftheuserdidnotspecify |
049 | //anerrorfile,thenerrorRedirectissettofalse. |
051 | //InitializetheprocessanditsStartInfoproperties. |
052 | netProcess= new Process(); |
053 | netProcess.StartInfo.FileName= "Net.exe" ; |
055 | //Buildthenetcommandargumentlist. |
056 | netProcess.StartInfo.Arguments=String.Format( "view{0}" , |
059 | //SetUseShellExecutetofalseforredirection. |
060 | netProcess.StartInfo.UseShellExecute= false ; |
062 | //Redirectthestandardoutputofthenetcommand. |
063 | //Thisstreamisreadasynchronouslyusinganeventhandler. |
064 | netProcess.StartInfo.RedirectStandardOutput= true ; |
065 | netProcess.OutputDataReceived+= new DataReceivedEventHandler(NetOutputDataHandler); |
066 | netOutput= new StringBuilder(); |
070 | //Redirecttheerroroutputofthenetcommand. |
071 | netProcess.StartInfo.RedirectStandardError= true ; |
072 | netProcess.ErrorDataReceived+= new DataReceivedEventHandler(NetErrorDataHandler); |
076 | //Donotredirecttheerroroutput. |
077 | netProcess.StartInfo.RedirectStandardError= false ; |
080 | Console.WriteLine( "/nStartingprocess:net{0}" , |
081 | netProcess.StartInfo.Arguments); |
084 | Console.WriteLine( "Errorswillbewrittentothefile{0}" , |
091 | //Starttheasynchronousreadofthestandardoutputstream. |
092 | netProcess.BeginOutputReadLine(); |
096 | //Starttheasynchronousreadofthestandard |
098 | netProcess.BeginErrorReadLine(); |
101 | //Letthenetcommandrun,collectingtheoutput. |
102 | netProcess.WaitForExit(); |
111 | //SeterrorsWrittentofalseifthestreamisnot |
112 | //open.Eithertherearenoerrors,ortheerror |
113 | //filecouldnotbeopened. |
117 | if (netOutput.Length>0) |
119 | //Iftheprocesswrotemorethanjust |
120 | //whitespace,writetheoutputtotheconsole. |
121 | Console.WriteLine( "/nPublicnetworksharesfromnetview:/n{0}/n" , |
127 | //Signalthattheerrorfilehadsomething |
129 | String[]errorOutput=File.ReadAllLines(netErrorFile); |
130 | if (errorOutput.Length>0) |
132 | Console.WriteLine( "/nThefollowingerroroutputwasappendedto{0}." , |
134 | foreach (StringerrLine in errorOutput) |
136 | Console.WriteLine( "{0}" ,errLine); |
146 | private static void NetOutputDataHandler( object sendingProcess, |
147 | DataReceivedEventArgsoutLine) |
149 | //Collectthenetviewcommandoutput. |
150 | if (!String.IsNullOrEmpty(outLine.Data)) |
152 | //Addthetexttothecollectedoutput. |
153 | netOutput.Append(Environment.NewLine+ "" +outLine.Data); |
157 | private static void NetErrorDataHandler( object sendingProcess, |
158 | DataReceivedEventArgserrLine) |
160 | //Writetheerrortexttothefileifthereissomething |
161 | //towriteandanerrorfilehasbeenspecified. |
163 | if (!String.IsNullOrEmpty(errLine.Data)) |
172 | streamError= new StreamWriter(netErrorFile, true ); |
176 | Console.WriteLine( "Couldnotopenerrorfile!" ); |
177 | Console.WriteLine(e.Message.ToString()); |
183 | //Writeaheadertothefileifthisisthefirst |
184 | //calltotheerroroutputhandler. |
185 | streamError.WriteLine(); |
186 | streamError.WriteLine(DateTime.Now.ToString()); |
187 | streamError.WriteLine( "NetViewerroroutput:" ); |
194 | //Writeredirectederrorstothefile. |
195 | streamError.WriteLine(errLine.Data); |
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理