如何在.net 中执行console命令
Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '创建一个新的进程对象
Dim myCmdProcess As New Process
'注册进程退出事件
'myCmdProcess.Exited += New System.EventHandler(myCmdProcess_exited)
AddHandler myCmdProcess.Exited, AddressOf myCmdProcess_exited
myCmdProcess.StartInfo.FileName = "cmd" '要执行的命令
'将参数传给要调用的应用程序 /C 执行字符串指定的命令然后终断 ,调用dir,同时将结果输出到应用程序文件夹下test.txt.
myCmdProcess.StartInfo.Arguments = "/C dir >test.txt"
myCmdProcess.StartInfo.RedirectStandardOutput = True
myCmdProcess.StartInfo.UseShellExecute = False
myCmdProcess.StartInfo.CreateNoWindow = True
myCmdProcess.EnableRaisingEvents = True
myCmdProcess.Start()
Console.Read()
End Sub
'进程退出时调用的方法
Private Sub myCmdProcess_exited(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Dim myFile As System.IO.StreamReader = New System.IO.StreamReader("test.txt")
Dim myString As String = myFile.ReadToEnd()
myFile.Close()
MessageBox.Show(myString)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub