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

王朝c#·作者佚名  2006-12-16
宽屏版  字体: |||超大  

Author:水如烟

Public Class ConsoleOutputClass ConsoleOutput

Private Sub New()Sub New()

End Sub

Private Shared gWorkingDirectory As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)

Public Shared Property WorkingDirectory()Property WorkingDirectory() As String

Get

Return gWorkingDirectory

End Get

Set(ByVal Value As String)

gWorkingDirectory = Value

End Set

End Property

Public Shared Function ExcuteCmd()Function ExcuteCmd(ByVal command As String) As String

Dim mResult As String = ''

Dim tmpProcess As New Process

With tmpProcess

With .StartInfo

.CreateNoWindow = True

.FileName = .EnvironmentVariables.Item('ComSpec')

.RedirectStandardOutput = True

.UseShellExecute = False

.Arguments = String.Format('/C {0}', command)

.WorkingDirectory = gWorkingDirectory

End With

Try

.Start()

.WaitForExit(5000)

mResult = .StandardOutput.ReadToEnd

Catch e As System.ComponentModel.Win32Exception

mResult = e.ToString

End Try

End With

Return mResult

End Function

End Class测试:

Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Console.WriteLine(ConsoleOutput.ExcuteCmd('NET TIME \\192.168.0.223'))

End Sub

'结果

'\\192.168.0.223 的当前时间是 2006-3-30 9:57

'

'命令成功完成。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有