水如烟

                 顺其自然,水到渠成 LzmTW

文或代码皆是面向初学者.我是爱好者,也是初学者.那些"文章",只按自己理解写,我是不知术语名词的.所以只供参考,也仅供参考.

导航

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

Posted on 2006-03-30 09:58  水如烟(LzmTW)  阅读(1739)  评论(1编辑  收藏  举报
Author:水如烟

Public Class ConsoleOutput

    
Private Sub New()
    
End Sub


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

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


    
Public Shared Function ExcuteCmd(ByVal command As StringAs 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(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
    '
    '命令成功完成。