Vb.net 将DOS命令响应捕获为字符串

Vb.net 将DOS命令响应捕获为字符串,vb.net,string,Vb.net,String,您好,我需要使用visual basic将发送到命令提示符的命令的响应捕获为字符串, 我想在捕捉到字符串后立即读取它。 如果您在以下目录“C:\”egsample C:\Pingfolder中有一个名为Pingfolder的文件夹,并且在该egsample中有一个名为“ping.txt”的txt文件,则以下代码可以工作:您需要这个C:\Pingfolder\ping.txt 此代码将ping响应写入ping.txt文件 Private Sub Button1_Click(ByVal sen

您好,我需要使用visual basic将发送到命令提示符的命令的响应捕获为字符串, 我想在捕捉到字符串后立即读取它。 如果您在以下目录“C:\”egsample C:\Pingfolder中有一个名为Pingfolder的文件夹,并且在该egsample中有一个名为“ping.txt”的txt文件,则以下代码可以工作:您需要这个C:\Pingfolder\ping.txt 此代码将ping响应写入ping.txt文件

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim NewProcess As New Process
        Dim StartInfoProcess As New System.Diagnostics.ProcessStartInfo
        StartInfoProcess.FileName = "cmd"
        StartInfoProcess.RedirectStandardInput = True
        StartInfoProcess.RedirectStandardOutput = True
        StartInfoProcess.UseShellExecute = False
        StartInfoProcess.CreateNoWindow = True
        NewProcess.StartInfo = StartInfoProcess
        NewProcess.Start()
        Dim SIOSW As System.IO.StreamWriter = NewProcess.StandardInput
        SIOSW.WriteLine("cd\")
        SIOSW.WriteLine("cd Pingfolder")
        SIOSW.WriteLine("ping www.google.com > ping.txt")
        SIOSW.WriteLine("Exit")
        SIOSW.Close()
    End Sub
在SIOSW.WriteLine上方的代码中(“ping www.google.com>ping.txt”) ping www.google.com,然后将响应保存到ping.txt”。 在上面的代码中,我希望将响应捕获为字符串,而不是将其写入ping.txt文件。 我需要这样的东西:

dim theresault as string
theresault = SIOSW.WriteLine("ping www.google.com")
messagebox.show(theresault)

您需要在NewProcess上侦听OutputDataReceived事件


请参阅此处的文档:

您需要侦听NewProcess上的OutputDataReceived事件


请参阅此处的文档:

以下内容允许我读取对字符串的响应

     Dim thestring As String
   Dim SIOSR As System.IO.StreamReader = NewProcess.StandardOutput
    thestring = SIOSR.ReadToEnd
下面的代码完整代码发送命令并将响应保存为字符串,然后将其显示为消息

Dim NewProcess As New Process
Dim StartInfoProcess As New System.Diagnostics.ProcessStartInfo
Dim thestring As String
StartInfoProcess.FileName = "cmd"
StartInfoProcess.RedirectStandardInput = True
StartInfoProcess.RedirectStandardOutput = True
StartInfoProcess.UseShellExecute = False
StartInfoProcess.CreateNoWindow = True
NewProcess.StartInfo = StartInfoProcess
NewProcess.Start()
Dim SIOSW As System.IO.StreamWriter = NewProcess.StandardInput
Dim SIOSR As System.IO.StreamReader = NewProcess.StandardOutput
SIOSW.WriteLine("ping www.google.com")
Threading.Thread.Sleep(15000)
SIOSW.WriteLine("Exit")
thestring = SIOSR.ReadToEnd
    MessageBox.Show(thestring)
SIOSW.Close()
SIOSR.Close()

下面允许我读取对字符串的响应

     Dim thestring As String
   Dim SIOSR As System.IO.StreamReader = NewProcess.StandardOutput
    thestring = SIOSR.ReadToEnd
下面的代码完整代码发送命令并将响应保存为字符串,然后将其显示为消息

Dim NewProcess As New Process
Dim StartInfoProcess As New System.Diagnostics.ProcessStartInfo
Dim thestring As String
StartInfoProcess.FileName = "cmd"
StartInfoProcess.RedirectStandardInput = True
StartInfoProcess.RedirectStandardOutput = True
StartInfoProcess.UseShellExecute = False
StartInfoProcess.CreateNoWindow = True
NewProcess.StartInfo = StartInfoProcess
NewProcess.Start()
Dim SIOSW As System.IO.StreamWriter = NewProcess.StandardInput
Dim SIOSR As System.IO.StreamReader = NewProcess.StandardOutput
SIOSW.WriteLine("ping www.google.com")
Threading.Thread.Sleep(15000)
SIOSW.WriteLine("Exit")
thestring = SIOSR.ReadToEnd
    MessageBox.Show(thestring)
SIOSW.Close()
SIOSR.Close()