Author:水如烟
 Public Class ConsoleOutput
Public Class ConsoleOutput

 Private Sub New()
    Private Sub New()
 End Sub
    End Sub

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

 Public Shared Property WorkingDirectory() As String
    Public Shared Property WorkingDirectory() As String
 Get
        Get
 Return gWorkingDirectory
            Return gWorkingDirectory
 End Get
        End Get
 Set(ByVal Value As String)
        Set(ByVal Value As String)
 gWorkingDirectory = Value
            gWorkingDirectory = Value
 End Set
        End Set
 End Property
    End Property

 Public Shared Function ExcuteCmd(ByVal command As String) As String
    Public Shared Function ExcuteCmd(ByVal command As String) As String
 Dim mResult As String = ""
        Dim mResult As String = ""

 Dim tmpProcess As New Process
        Dim tmpProcess As New Process
 With tmpProcess
        With tmpProcess
 With .StartInfo
            With .StartInfo
 .CreateNoWindow = True
                .CreateNoWindow = True
 .FileName = .EnvironmentVariables.Item("ComSpec")
                .FileName = .EnvironmentVariables.Item("ComSpec")
 .RedirectStandardOutput = True
                .RedirectStandardOutput = True
 .UseShellExecute = False
                .UseShellExecute = False
 .Arguments = String.Format("/C {0}", command)
                .Arguments = String.Format("/C {0}", command)
 .WorkingDirectory = gWorkingDirectory
                .WorkingDirectory = gWorkingDirectory
 End With
            End With
 Try
            Try
 .Start()
                .Start()
 .WaitForExit(5000)
                .WaitForExit(5000)
 mResult = .StandardOutput.ReadToEnd
                mResult = .StandardOutput.ReadToEnd
 Catch e As System.ComponentModel.Win32Exception
            Catch e As System.ComponentModel.Win32Exception
 mResult = e.ToString
                mResult = e.ToString
 End Try
            End Try
 End With
        End With

 Return mResult
        Return mResult
 End Function
    End Function

 End Class
测试:
End Class
测试:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Private 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"))
        Console.WriteLine(ConsoleOutput.ExcuteCmd("NET TIME \\192.168.0.223"))
 End Sub
    End Sub
 '结果
    '结果
 '\\192.168.0.223 的当前时间是 2006-3-30 9:57
    '\\192.168.0.223 的当前时间是 2006-3-30 9:57
 '
    '
 '命令成功完成。
    '命令成功完成。
 Public Class ConsoleOutput
Public Class ConsoleOutput
 Private Sub New()
    Private Sub New() End Sub
    End Sub
 Private Shared gWorkingDirectory As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
    Private Shared gWorkingDirectory As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
 Public Shared Property WorkingDirectory() As String
    Public Shared Property WorkingDirectory() As String Get
        Get Return gWorkingDirectory
            Return gWorkingDirectory End Get
        End Get Set(ByVal Value As String)
        Set(ByVal Value As String) gWorkingDirectory = Value
            gWorkingDirectory = Value End Set
        End Set End Property
    End Property
 Public Shared Function ExcuteCmd(ByVal command As String) As String
    Public Shared Function ExcuteCmd(ByVal command As String) As String Dim mResult As String = ""
        Dim mResult As String = ""
 Dim tmpProcess As New Process
        Dim tmpProcess As New Process With tmpProcess
        With tmpProcess With .StartInfo
            With .StartInfo .CreateNoWindow = True
                .CreateNoWindow = True .FileName = .EnvironmentVariables.Item("ComSpec")
                .FileName = .EnvironmentVariables.Item("ComSpec") .RedirectStandardOutput = True
                .RedirectStandardOutput = True .UseShellExecute = False
                .UseShellExecute = False .Arguments = String.Format("/C {0}", command)
                .Arguments = String.Format("/C {0}", command) .WorkingDirectory = gWorkingDirectory
                .WorkingDirectory = gWorkingDirectory End With
            End With Try
            Try .Start()
                .Start() .WaitForExit(5000)
                .WaitForExit(5000) mResult = .StandardOutput.ReadToEnd
                mResult = .StandardOutput.ReadToEnd Catch e As System.ComponentModel.Win32Exception
            Catch e As System.ComponentModel.Win32Exception mResult = e.ToString
                mResult = e.ToString End Try
            End Try End With
        End With
 Return mResult
        Return mResult End Function
    End Function
 End Class
End Class Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Private 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"))
        Console.WriteLine(ConsoleOutput.ExcuteCmd("NET TIME \\192.168.0.223")) End Sub
    End Sub '结果
    '结果 '\\192.168.0.223 的当前时间是 2006-3-30 9:57
    '\\192.168.0.223 的当前时间是 2006-3-30 9:57 '
    ' '命令成功完成。
    '命令成功完成。 
                    
                
 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号