HOW TO:擷取DOS命令輸出結果

來源:互聯網
上載者:User
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
    '
    '命令成功完成。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.