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

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
     '
     ' 命令成功完成。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值