HOW TO:获取DOS命令输出结果

PublicClassConsoleOutputClassConsoleOutput

PrivateSubNew()SubNew()
EndSub
PrivateSharedgWorkingDirectoryAsString=Environment.GetFolderPath(Environment.SpecialFolder.Personal)

PublicSharedPropertyWorkingDirectory()PropertyWorkingDirectory()AsString
Get
ReturngWorkingDirectory
EndGet
Set(ByValValueAsString)
gWorkingDirectory=Value
EndSet
EndProperty

PublicSharedFunctionExcuteCmd()FunctionExcuteCmd(ByValcommandAsString)AsString
DimmResultAsString=""
DimtmpProcessAsNewProcess
WithtmpProcess
With.StartInfo
.CreateNoWindow=True
.FileName=.EnvironmentVariables.Item("ComSpec")
.RedirectStandardOutput=True
.UseShellExecute=False
.Arguments=String.Format("/C{0}",command)
.WorkingDirectory=gWorkingDirectory
EndWith
Try
.Start()
.WaitForExit(5000)
mResult=.StandardOutput.ReadToEnd
CatcheAsSystem.ComponentModel.Win32Exception
mResult=e.ToString
EndTry
EndWith
ReturnmResult
EndFunction
EndClass
测试:

PrivateSubButton1_Click()SubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
Console.WriteLine(ConsoleOutput.ExcuteCmd("NETTIME//192.168.0.223"))
EndSub
'结果
'//192.168.0.223的当前时间是2006-3-309:57
'
'命令成功完成。
本文介绍了一种通过VB.NET程序获取DOS命令输出的方法。利用该方法,可以将命令如NET TIME的输出结果读取并显示出来。示例中包含了完整的代码实现。
5391

被折叠的 条评论
为什么被折叠?



