您的位置:首页 > 其它

执行cmd并返回程序结果

2009-12-16 21:35 323 查看
001
//Definethenamespacesusedbythissample.
002
using
System;
003
using
System.Text;
004
using
System.Globalization;
005
using
System.IO;
006
using
System.Diagnostics;
007
using
System.Threading;
008
using
System.ComponentModel;
009
010
011
namespace
ProcessAsyncStreamSamples
012
{
013
014
class
ProcessNetStreamRedirection
015
{
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
;
022
023
public
static
void
RedirectNetCommandStreams()
024
{
025
StringnetArguments;
026
ProcessnetProcess;
027
028
//Gettheinputcomputername.
029
Console.WriteLine(
"Enterthecomputernameforthenetviewcommand:"
);
030
netArguments=Console.ReadLine().ToUpper(CultureInfo.InvariantCulture);
031
if
(String.IsNullOrEmpty(netArguments))
032
{
033
//Defaulttothehelpcommandifthereisnotaninputargument.
034
netArguments=
"/?"
;
035
}
036
037
//Checkiferrorsshouldberedirectedtoafile.
038
errorsWritten=
false
;
039
Console.WriteLine(
"Enterafullyqualifiedpathtoanerrorlogfile"
);
040
Console.WriteLine(
"orjustpressEntertowriteerrorstoconsole:"
);
041
netErrorFile=Console.ReadLine().ToUpper(CultureInfo.InvariantCulture);
042
if
(!String.IsNullOrEmpty(netErrorFile))
043
{
044
errorRedirect=
true
;
045
}
046
047
//Notethatatthispoint,netArgumentsandnetErrorFile
048
//aresetwithuserinput.Iftheuserdidnotspecify
049
//anerrorfile,thenerrorRedirectissettofalse.
050
051
//InitializetheprocessanditsStartInfoproperties.
052
netProcess=
new
Process();
053
netProcess.StartInfo.FileName=
"Net.exe"
;
054
055
//Buildthenetcommandargumentlist.
056
netProcess.StartInfo.Arguments=String.Format(
"view{0}"
,
057
netArguments);
058
059
//SetUseShellExecutetofalseforredirection.
060
netProcess.StartInfo.UseShellExecute=
false
;
061
062
//Redirectthestandardoutputofthenetcommand.
063
//Thisstreamisreadasynchronouslyusinganeventhandler.
064
netProcess.StartInfo.RedirectStandardOutput=
true
;
065
netProcess.OutputDataReceived+=
new
DataReceivedEventHandler(NetOutputDataHandler);
066
netOutput=
new
StringBuilder();
067
068
if
(errorRedirect)
069
{
070
//Redirecttheerroroutputofthenetcommand.
071
netProcess.StartInfo.RedirectStandardError=
true
;
072
netProcess.ErrorDataReceived+=
new
DataReceivedEventHandler(NetErrorDataHandler);
073
}
074
else
075
{
076
//Donotredirecttheerroroutput.
077
netProcess.StartInfo.RedirectStandardError=
false
;
078
}
079
080
Console.WriteLine(
"/nStartingprocess:net{0}"
,
081
netProcess.StartInfo.Arguments);
082
if
(errorRedirect)
083
{
084
Console.WriteLine(
"Errorswillbewrittentothefile{0}"
,
085
netErrorFile);
086
}
087
088
//Starttheprocess.
089
netProcess.Start();
090
091
//Starttheasynchronousreadofthestandardoutputstream.
092
netProcess.BeginOutputReadLine();
093
094
if
(errorRedirect)
095
{
096
//Starttheasynchronousreadofthestandard
097
//errorstream.
098
netProcess.BeginErrorReadLine();
099
}
100
101
//Letthenetcommandrun,collectingtheoutput.
102
netProcess.WaitForExit();
103
104
if
(streamError!=
null
)
105
{
106
//Closetheerrorfile.
107
streamError.Close();
108
}
109
else
110
{
111
//SeterrorsWrittentofalseifthestreamisnot
112
//open.Eithertherearenoerrors,ortheerror
113
//filecouldnotbeopened.
114
errorsWritten=
false
;
115
}
116
117
if
(netOutput.Length>0)
118
{
119
//Iftheprocesswrotemorethanjust
120
//whitespace,writetheoutputtotheconsole.
121
Console.WriteLine(
"/nPublicnetworksharesfromnetview:/n{0}/n"
,
122
netOutput);
123
}
124
125
if
(errorsWritten)
126
{
127
//Signalthattheerrorfilehadsomething
128
//writtentoit.
129
String[]errorOutput=File.ReadAllLines(netErrorFile);
130
if
(errorOutput.Length>0)
131
{
132
Console.WriteLine(
"/nThefollowingerroroutputwasappendedto{0}."
,
133
netErrorFile);
134
foreach
(StringerrLine
in
errorOutput)
135
{
136
Console.WriteLine(
"{0}"
,errLine);
137
}
138
}
139
Console.WriteLine();
140
}
141
142
netProcess.Close();
143
144
}
145
146
private
static
void
NetOutputDataHandler(
object
sendingProcess,
147
DataReceivedEventArgsoutLine)
148
{
149
//Collectthenetviewcommandoutput.
150
if
(!String.IsNullOrEmpty(outLine.Data))
151
{
152
//Addthetexttothecollectedoutput.
153
netOutput.Append(Environment.NewLine+
""
+outLine.Data);
154
}
155
}
156
157
private
static
void
NetErrorDataHandler(
object
sendingProcess,
158
DataReceivedEventArgserrLine)
159
{
160
//Writetheerrortexttothefileifthereissomething
161
//towriteandanerrorfilehasbeenspecified.
162
163
if
(!String.IsNullOrEmpty(errLine.Data))
164
{
165
if
(!errorsWritten)
166
{
167
if
(streamError==
null
)
168
{
169
//Openthefile.
170
try
171
{
172
streamError=
new
StreamWriter(netErrorFile,
true
);
173
}
174
catch
(Exceptione)
175
{
176
Console.WriteLine(
"Couldnotopenerrorfile!"
);
177
Console.WriteLine(e.Message.ToString());
178
}
179
}
180
181
if
(streamError!=
null
)
182
{
183
//Writeaheadertothefileifthisisthefirst
184
//calltotheerroroutputhandler.
185
streamError.WriteLine();
186
streamError.WriteLine(DateTime.Now.ToString());
187
streamError.WriteLine(
"NetViewerroroutput:"
);
188
}
189
errorsWritten=
true
;
190
}
191
192
if
(streamError!=
null
)
193
{
194
//Writeredirectederrorstothefile.
195
streamError.WriteLine(errLine.Data);
196
streamError.Flush();
197
}
198
}
199
}
200
}
201
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: