VB .NET 2003 platform: How can I capture CMD output?
Example:
send "ipconfig" to CMD. Present the output (result) of the command in Text1 TextBox. Thank you all, O.P.you can start an expernal process by using System.Diagnostics.Process:
Start of class:
Private WithEvents m_process As System.Diagnostics.Process
In your method:
pi = New System.Diagnostics.ProcessStartInfo
pi.Arguments = ...
pi.FileName = ...
pi.WorkingDirectory = ...
pi.UseShellExecute = True
pi.RedirectStandardOutput = True
pi.RedirectStandardError = True
pi.CreateNoWindow = True
m_process = New Process
m_process.StartInfo = pi
m_process.Start()
m_process.BeginOutputReadLine()
m_process.BeginErrorReadLine()
m_process.WaitForExit()
Listen to the output/error by
Private Sub m_process_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles m_process.OutputDataReceived
and
Private Sub m_process_ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles m_process.ErrorDataReceived
Thank you Tobias, with your help I was managed to build a full solution to my needs.
For those of you who need such solution, I have used a regular Form with a simple button and a Text Box to obtain the output of a CMD command:
' Under the Relevant Button click method:
' Note: One should add relevant referrences in order for it to work
' I have added most of them without the need to figure out which one specifically :)
Dim myProcess As Process = New Process
Dim s As String
myProcess.StartInfo.FileName = "c:\windows\system32\cmd.exe"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.CreateNoWindow = True
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.Start()
Dim sIn As System.IO.StreamWriter = myProcess.StandardInput
Dim sOut As System.IO.StreamReader = myProcess.StandardOutput
Dim sErr As System.IO.StreamReader = myProcess.StandardError
sIn.AutoFlush = True
sIn.Write("ipconfig" & System.Environment.NewLine)
sIn.Write("exit" & System.Environment.NewLine)
s = sOut.ReadToEnd()
If Not myProcess.HasExited Then
myProcess.Kill()
End If
cmdOutputTextBox.Text = s
cmdOutputTextBox.Visible = True
sIn.Close()
sOut.Close()
sErr.Close()
myProcess.Close()
Thanks - I hope it helped you.
O.P
精彩评论