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

I use gnokii to send SMS messages.

My VB codes:

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

Note:

  • I tried redirecting the output to a .txt file, but the .txt file seems empty. In addition, the program may have to send several SMS messages every second, so creating .txt is not possible.

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

  • I need a conclusion to check if the SMS was sent successfully.

  • I tried using (codes below), but that didn't work either; not shown.

    Exe function (ByVal filename, ByVal arguments)

     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 

    Final function

+4
source share
4 answers

To send the 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" 
+1
source

Try the following:

  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 
+2
source

You can use this 100% work, but it will only show 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
source

I could suggest something like this. This is similar to what someone else has posted, but it offers a bit 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 
0
source

All Articles