获取DOS命令输出结果

Public Class ConsoleOutput

    Private 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
    '
    '命令成功完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值