在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 服务

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...