微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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:\Windows\system32\cmd.exe"
p.StartInfo.Arguments = "/k C:\Windows\system32\ipconfig.exe /all"
‘ p.StartInfo.Arguments = "regsvr32.exe "
p.Start()

3、方法


System.Diagnostics.Process.Start("C:\Windows\system32\cmd.exe",
"/k C:\Windows\system32\ipconfig.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 服务

原文地址:https://www.jb51.cc/vb/260220.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐