2011-06-18 316 views
4

我正在使用gnokii发送短信。如何从cmd shell读取VB.NET中的cmd输出?

我的VB代码:

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

注意要点:

  1. 我曾尝试将输出重定向到一个.txt文件,但.txt文件显示为空。此外,该程序可能不得不每秒发送多个SMS,因此创建一个.txt文件是不可行的。

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

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

  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 
    

    端功能

+0

如果您将xCMD更改为具有> c:\ xxx.txt,xxx.txt的内容是否包含您想要的输出? – BugFinder

+0

也许[此链接](http://stackoverflow.com/questions/2162078/pick-up-strings-from-cmd-command-process-startinfo)将有所帮助。 –

+0

@BugFinder不要它 – Joyce

回答

1

输出发送到文本文件,(该最好的解决方案,我可以找到)

REPLACE

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" 
2

试试这个:

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 
+0

嗯,是不是我所引用的..大声笑,但仍然,感谢您的帮助:) – Joyce

+2

'.WaitForExit()' – aligray

0

你可以使用这个100级%的作品,但它只会告诉你结果

如何显示在vb.net壳结果:

'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