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

4

我正在使用gnokii发送短信。

我的VB代码:

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

注意事项:

  1. I did try to redirect the output to a .txt file but the .txt file appears to be empty. Besides, the program may have to send out multiple SMSes every second, so creating a .txt is not feasible.

  2. Process.Start() is not feasible because I have to check if gnokii.exe is running.

  3. I need the output to check if the SMS is sent successfully.

  4. I tried using (codes below), but it didn't work either; no output was shown.

    Function exe(ByVal fileName, ByVal args)

    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
    

    End Function


如果你将你的 xCMD 改为 > c:\xxx.txt,那么 xxx.txt 的内容是否包含了你想要的输出? - BugFinder
好的,大家好,昨天我找到了如何将输出保存到 .txt 文件中的答案。只需将“... > xxx.txt”更改为“... 2> xxx.txt”。感谢所有提供帮助的人 :) - Joyce
1
好的,2 是错误输出,而不是标准输出,这就是为什么它不能与其他方法一起使用的原因。 - BugFinder
@BugFinder 是的,我一直认为2>和>是一回事,所以我并没有想到数字会有影响。 - Joyce
现在你知道了,2>是不同的 :) - BugFinder
显示剩余3条评论
4个回答

3

试试这个:

    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

嗯,这不是我引用的吗..哈哈,但还是谢谢你的帮助 :) - Joyce
刚刚看到这个评论,居然是9年前的哈哈,区别在于.WaitForExit() - aligray

1

将输出发送到 .txt 文件中(这是我能找到的最佳解决方案)

替换

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

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

0

您可以使用这个百分之百的方法,但它只会向您显示结果。

如何在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


0
我个人可能会建议这样做。这与其他人发布的内容类似,但我认为它提供了更多的功能。
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:\temp\output.txt")
    Dim read As System.IO.StreamReader
    read = File.OpenText("c:\temp\output.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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接