如何从cmd shell读取VB.NET中的cmd输出



我正在使用gnokii发送短信。

我的VB代码:

Dim xCmd As String
xCmd = "cmd.exe /c echo msgcontent "| c:gnokiignokii.exe --sendsms 12345678"
Shell(xCmd)

注意事项:

  1. 我确实尝试将输出重定向到.txt文件,但.txt文件似乎是空的。此外,该程序可能每秒要发送多条短信,因此创建.txt是不可行的。

  2. 过程。Start()不可行,因为我必须检查gnokii.exe是否正在运行。

  3. 我需要输出来检查短信是否发送成功。

  4. 我尝试使用(下面的代码),但也不起作用;没有显示输出。

    函数exe(ByVal文件名,ByVal参数)

    Dim p As Process = New Process
    Dim output As String
    With p
        .StartInfo.CreateNoWindow = True
        .StartInfo.UseShellExecute = False
        .StartInfo.RedirectStandardOutput = True
        .StartInfo.FileName = fileName
        .StartInfo.Arguments = args
        .Start()
        output = .StandardOutput.ReadToEnd
    End With
    Return output
    

    终端功能

试试这个:

    Dim p As Process = New Process
    Dim output As String
    With p
        .StartInfo.CreateNoWindow = True
        .StartInfo.RedirectStandardOutput = True
        .StartInfo.UseShellExecute = False
        .StartInfo.FileName = fileName
        .StartInfo.Arguments = args
        .Start()
        output = .StandardOutput.ReadToEnd
        .WaitForExit()
    End With
    Return output

要将输出发送到.txt文件,(我能找到的最好的解决方案)

更换

xCmd = "cmd.exe /c echo msgcontent "| c:gnokiignokii.exe --sendsms 12345678 > file.txt"

使用

xCmd = "cmd.exe /c echo msgcontent "| c:gnokiignokii.exe --sendsms 12345678 2> file.txt"

你可以使用这个100%的工作,但它只会向你显示的结果

如何在vb.net中显示shell结果:

'create 1 textbox1
'create 1 button1
'create 1 richtextbox1
'in the start up directory of this program make a file could 123.text
'------------------------------------------------------------------------
Dim read As System.IO.StreamReader
read = File.OpenText(Application.StartupPath & "123.text")
Shell("cmd.exe /c" & TextBox1.Text + ">123.text")
Do Until read.EndOfStream
    RichTextBox1.Text = read.ReadLine & vbCrLf
Loop
'--------------------------------------------------------------------------
'you can add on the top to create the file if it does not exists,   
If IO.File.Exists(Application.StartupPath & "123.text") = False Then
    IO.File.Create(Application.StartupPath & "123.text")
End If
'-------------------------------------------------------------------------

此链接中也提供该代码http://pastebin.com/iEhv61jG

我自己可能会提出这样的建议。这与其他人发布的内容类似,但我认为它提供了更多的功能。

Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Shell("cmd.exe /c " & TextBox1.Text + " > c:tempoutput.txt")
    Dim read As System.IO.StreamReader
    read = File.OpenText("c:tempoutput.txt")
    RichTextBox1.Clear()
    Do Until read.EndOfStream
        RichTextBox1.Text += read.ReadLine & vbCrLf
    Loop
    RichTextBox1.Select(RichTextBox1.Text.Length, 0)
    RichTextBox1.ScrollToCaret()
End Sub
End Class

最新更新