Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. How can I send command line and read the responses?

How can I send command line and read the responses?

Scheduled Pinned Locked Moved Visual Basic
questionlinuxtutorialworkspace
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ATC
    wrote on last edited by
    #1

    I have to execute an application (ie: c:\applications\task1.exe). I have to send some different commands and wait for the responses. Some responses are fast and has only few lines, some will take long time to execute but response every second with a dot until it completed I would line to send command line through VB environment (might be with Shell (...) command?), but able to capture the responses without waiting it completed (to monitor its processes) Anyone can show me how to do it? Many thanks

    D D 2 Replies Last reply
    0
    • A ATC

      I have to execute an application (ie: c:\applications\task1.exe). I have to send some different commands and wait for the responses. Some responses are fast and has only few lines, some will take long time to execute but response every second with a dot until it completed I would line to send command line through VB environment (might be with Shell (...) command?), but able to capture the responses without waiting it completed (to monitor its processes) Anyone can show me how to do it? Many thanks

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      There's a bunch of code behind that. But, you're launching the process with the Process class, then redirecting the StandardInput and StandardOutput streams so your code can capture the output of the console app. I don't have an example handy because I'm walking out the door right now...

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      1 Reply Last reply
      0
      • A ATC

        I have to execute an application (ie: c:\applications\task1.exe). I have to send some different commands and wait for the responses. Some responses are fast and has only few lines, some will take long time to execute but response every second with a dot until it completed I would line to send command line through VB environment (might be with Shell (...) command?), but able to capture the responses without waiting it completed (to monitor its processes) Anyone can show me how to do it? Many thanks

        D Offline
        D Offline
        DigiOz Multimedia
        wrote on last edited by
        #3

        ATC wrote:

        I have to execute an application (ie: c:\applications\task1.exe). I have to send some different commands and wait for the responses. Some responses are fast and has only few lines, some will take long time to execute but response every second with a dot until it completed I would line to send command line through VB environment (might be with Shell (...) command?), but able to capture the responses without waiting it completed (to monitor its processes) Anyone can show me how to do it?

        Here is an example, which executes the "cmd.exe" (command line executable), gets a directory listing, and displays the result in a messagebox:

        Dim ps1 As New Process
        ps1.StartInfo.FileName = "cmd.exe"
        ps1.StartInfo.WindowStyle = ProcessWindowStyle.Normal
        ps1.StartInfo.CreateNoWindow = False
        ps1.StartInfo.UseShellExecute = False
        ps1.StartInfo.RedirectStandardOutput = True
        ps1.StartInfo.RedirectStandardInput = True
        ps1.Start()

        Dim s As String
        Dim sIn As StreamWriter = ps1.StandardInput
        Dim sOut As StreamReader = ps1.StandardOutput
        sIn.AutoFlush = True

        sIn.Write("dir /p" & System.Environment.NewLine)
        s = sOut.ReadToEnd()
        MessageBox.Show(s)

        Don't forget to "Imports System.IO" for this. I hope this helps. :)

        Pete Soheil DigiOz Multimedia http://www.digioz.com

        A 1 Reply Last reply
        0
        • D DigiOz Multimedia

          ATC wrote:

          I have to execute an application (ie: c:\applications\task1.exe). I have to send some different commands and wait for the responses. Some responses are fast and has only few lines, some will take long time to execute but response every second with a dot until it completed I would line to send command line through VB environment (might be with Shell (...) command?), but able to capture the responses without waiting it completed (to monitor its processes) Anyone can show me how to do it?

          Here is an example, which executes the "cmd.exe" (command line executable), gets a directory listing, and displays the result in a messagebox:

          Dim ps1 As New Process
          ps1.StartInfo.FileName = "cmd.exe"
          ps1.StartInfo.WindowStyle = ProcessWindowStyle.Normal
          ps1.StartInfo.CreateNoWindow = False
          ps1.StartInfo.UseShellExecute = False
          ps1.StartInfo.RedirectStandardOutput = True
          ps1.StartInfo.RedirectStandardInput = True
          ps1.Start()

          Dim s As String
          Dim sIn As StreamWriter = ps1.StandardInput
          Dim sOut As StreamReader = ps1.StandardOutput
          sIn.AutoFlush = True

          sIn.Write("dir /p" & System.Environment.NewLine)
          s = sOut.ReadToEnd()
          MessageBox.Show(s)

          Don't forget to "Imports System.IO" for this. I hope this helps. :)

          Pete Soheil DigiOz Multimedia http://www.digioz.com

          A Offline
          A Offline
          ATC
          wrote on last edited by
          #4

          Thanks, I also hope so, but I got errors on Dim sIn As StreamWriter (not defined). might be I need add some kind of Import?

          D 1 Reply Last reply
          0
          • A ATC

            Thanks, I also hope so, but I got errors on Dim sIn As StreamWriter (not defined). might be I need add some kind of Import?

            D Offline
            D Offline
            DigiOz Multimedia
            wrote on last edited by
            #5

            ATC wrote:

            Thanks, I also hope so, but I got errors on Dim sIn As StreamWriter (not defined). might be I need add some kind of Import?

            Correct. At the very "TOP" of your Windows Form, type "Imports System.IO". That should fix your problem.

            Pete Soheil DigiOz Multimedia http://www.digioz.com

            A 1 Reply Last reply
            0
            • D DigiOz Multimedia

              ATC wrote:

              Thanks, I also hope so, but I got errors on Dim sIn As StreamWriter (not defined). might be I need add some kind of Import?

              Correct. At the very "TOP" of your Windows Form, type "Imports System.IO". That should fix your problem.

              Pete Soheil DigiOz Multimedia http://www.digioz.com

              A Offline
              A Offline
              ATC
              wrote on last edited by
              #6

              Hi Pete, Thanks for the reply, however when I try to run as it is (without compiling error), it launches cmd (ms-dos window) empty then stays there forever until I close the ms-dos window then it shows the responses ( MessageBox.Show(s) ) I do not know why I have to close the ms-dos window manually, can we close it automatically? Thanks -- modified at 10:29 Tuesday 16th October, 2007

              D 1 Reply Last reply
              0
              • A ATC

                Hi Pete, Thanks for the reply, however when I try to run as it is (without compiling error), it launches cmd (ms-dos window) empty then stays there forever until I close the ms-dos window then it shows the responses ( MessageBox.Show(s) ) I do not know why I have to close the ms-dos window manually, can we close it automatically? Thanks -- modified at 10:29 Tuesday 16th October, 2007

                D Offline
                D Offline
                DigiOz Multimedia
                wrote on last edited by
                #7

                ATC wrote:

                Thanks for the reply, however when I try to run as it is (without compiling error), it launches cmd (ms-dos window) empty then stays there forever until I close the ms-dos window then it shows the responses ( MessageBox.Show(s) ) I do not know why I have to close the ms-dos window manually, can we close it automatically?

                Its because of this statement: s = sOut.ReadToEnd() Take that line out and it will close the DOS screen on its own. :)

                Pete Soheil DigiOz Multimedia http://www.digioz.com

                A 1 Reply Last reply
                0
                • D DigiOz Multimedia

                  ATC wrote:

                  Thanks for the reply, however when I try to run as it is (without compiling error), it launches cmd (ms-dos window) empty then stays there forever until I close the ms-dos window then it shows the responses ( MessageBox.Show(s) ) I do not know why I have to close the ms-dos window manually, can we close it automatically?

                  Its because of this statement: s = sOut.ReadToEnd() Take that line out and it will close the DOS screen on its own. :)

                  Pete Soheil DigiOz Multimedia http://www.digioz.com

                  A Offline
                  A Offline
                  ATC
                  wrote on last edited by
                  #8

                  Oopps, in that case how can I read the return from the application? :doh:

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups