加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在vb2010.net下的命令行执行方法

发布时间:2020-12-16 22:36:11 所属栏目:大数据 来源:网络整理
导读:1、方法一 VB.Net下异步调用命令行的函数是Shell(),异步的意思就是程序执行到这个Shell()函数,不会等待其执行完毕就会立即跑到Shell()之后去运行剩下的语句。 如果我们需要等待命令行程序执行完毕之后再继续运行,可以使用如下函数: Public Function Exec

1、方法一

VB.Net下异步调用命令行的函数是Shell(),异步的意思就是程序执行到这个Shell()函数,不会等待其执行完毕就会立即跑到Shell()之后去运行剩下的语句。

如果我们需要等待命令行程序执行完毕之后再继续运行,可以使用如下函数:

Public Function ExecuteCmd(ByVal cmd As String)
Dim startInfo As New ProcessStartInfo("cmd.exe")'调用程序名
With startInfo
.Arguments = "/C " + cmd '调用命令 CMD
.RedirectStandardError = True
.RedirectStandardOutput = True
.UseShellExecute = False
.CreateNoWindow = True
End With

Dim p As Process = Process.Start(startInfo)
Dim strOutput As String = p.StandardOutput.ReadToEnd()
Dim strError As String = p.StandardError.ReadToEnd()
p.WaitForExit()
If (strOutput.Length <> 0) Then
Return strOutput
ElseIf (strError.Length <> 0) Then
Return strError
End If
Return ""
End Function

函数返回值就是命令行执行时屏幕回显的内容。

2、方法二


Dim p As Process = New Process
p.StartInfo.FileName = "C:Windowssystem32cmd.exe"
p.StartInfo.Arguments = "/k C:Windowssystem32ipconfig.exe /all"
‘ p.StartInfo.Arguments = "regsvr32.exe "
p.Start()

3、方法三


System.Diagnostics.Process.Start("C:Windowssystem32cmd.exe",
"/k C:Windowssystem32ipconfig.exe /all")

4、方法四

可以通过Process类和ProcessStartInfo类实现,也可以使用管道等操作,如:> |等。下面就是一个例子

<p>System.Diagnostics.Process.Start(“CMD.exe”,“/c net send 192.168.3.6 你今天过的好吗?”)</p><p>System.Diagnostics.Process.Start(“cmd.exe”,“/c foo.exe -arg” +“| bar.exe”)</p>
注意:net send 需要启用 Messenger 服务

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读