How to I read cmd output in VB.NET from cmd shell?

23.2k views Asked by At

I am using gnokii to send out SMSes.

My VB Codes:

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

Points to note:

  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

4

There are 4 answers

0
Joyce On BEST ANSWER

To send output to a .txt file, (the best solution I can find)

REPLACE

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

WITH

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

You can use this 100% works but it will only show you the results

How to show shell results in 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
'-------------------------------------------------------------------------

The code is also available at this link http://pastebin.com/iEhv61jG

0
Skotte On

I might suggest something like this, myself. This is similar to what someone else posted, but it offers a little more functionality, I think.

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
2
aligray On

Try this:

    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