4

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

8
  • If you change your xCMD to have a > c:\xxx.txt does the content of xxx.txt contain the output you wanted? Commented Jun 18, 2011 at 16:56
  • Maybe this link will help. Commented Jun 18, 2011 at 17:39
  • Okay, hi all, yesterday I found an answer on how to save the output into the .txt file. Simply change "... > xxx.txt" to "... 2> xxx.txt" Thanks for all the help that was given :) Commented Jun 19, 2011 at 23:48
  • Um, how do I do that? (New here) Commented Jun 20, 2011 at 0:08
  • 1
    OK, 2 is error out, not std out, thats why it wasnt working with other methods. Commented Jun 20, 2011 at 10:41

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Um, isn't that what I quoted.. lol, but still, thanks for the help :)
Just seen this comment now after 9 years lol, the difference was .WaitForExit().
1

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"

Comments

0

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.