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 to type on command window at an instance?

How to type on command window at an instance?

Scheduled Pinned Locked Moved Visual Basic
csharpadobelinuxhelp
12 Posts 2 Posters 1 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.
  • B barkanb

    My platform: VB.NET Windows Form My case: DOS program mon.exe runs on command window. This program enables you to monitor a flash ram. To monitor ipno 10.0.0.90 from 4000 port you just type: C:\>mon 10.0.0.90 4000 Which can also be performed by VB.NET with command line: Shell("mon " & ipno & " 4000", AppWinStyle.MaximizedFocus, True) After mon.exe executes you are monitoring the ipno and a command line occurs. 10.0.0.90\> To change the ipno you type SET IP newIP: 10.0.0.90\> SET IP 10.0.0.44 At this point, I am not able to execute SET IP command line. I tried to create batch file but it didn't work. I tried :confused:: System.Console.Write("SET IP" & newIP) 'but it didn't work. I am trying to develop a Windows Form to change the ip of a special system. Can anyone help me? Should I work on batch files? Is there a way to type on command window while shell command is executing? :confused: I will be really appriciated for an answer. B.B.

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

    You can't type into a Shell'd process from VB.NET. You have to create your own process using the Process class. You can then redirect the StandardInput stream so you can have your code type in the command window. You can't do this using Shell.

    Dave Kreskowiak Microsoft MVP - Visual Basic

    B 2 Replies Last reply
    0
    • D Dave Kreskowiak

      You can't type into a Shell'd process from VB.NET. You have to create your own process using the Process class. You can then redirect the StandardInput stream so you can have your code type in the command window. You can't do this using Shell.

      Dave Kreskowiak Microsoft MVP - Visual Basic

      B Offline
      B Offline
      barkanb
      wrote on last edited by
      #3

      Thanks. I will try. It will take some time. But will post the result.;) B.B.

      1 Reply Last reply
      0
      • D Dave Kreskowiak

        You can't type into a Shell'd process from VB.NET. You have to create your own process using the Process class. You can then redirect the StandardInput stream so you can have your code type in the command window. You can't do this using Shell.

        Dave Kreskowiak Microsoft MVP - Visual Basic

        B Offline
        B Offline
        barkanb
        wrote on last edited by
        #4

        Is it right below? To start monitoring? It does not seem to work properly. Dim objProcess As New Process ' Start the Command and redirect the output objProcess.StartInfo.UseShellExecute = False objProcess.StartInfo.RedirectStandardOutput = True objProcess.StartInfo.CreateNoWindow = False objProcess.StartInfo.RedirectStandardError = True objProcess.StartInfo.WorkingDirectory = CurDir() objProcess.StartInfo.FileName() = "mon" objProcess.StartInfo.Arguments = " 10.0.0.99 4000" objProcess.Start()

        D 1 Reply Last reply
        0
        • B barkanb

          Is it right below? To start monitoring? It does not seem to work properly. Dim objProcess As New Process ' Start the Command and redirect the output objProcess.StartInfo.UseShellExecute = False objProcess.StartInfo.RedirectStandardOutput = True objProcess.StartInfo.CreateNoWindow = False objProcess.StartInfo.RedirectStandardError = True objProcess.StartInfo.WorkingDirectory = CurDir() objProcess.StartInfo.FileName() = "mon" objProcess.StartInfo.Arguments = " 10.0.0.99 4000" objProcess.Start()

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

          Well, you got most of it right. The only stream you didn't redirect was StandardInput. That's the keyboard stream that let's you "type" something into that console session.

          Dim objProcess As New Process
          With objProcess.StartInfo
              .UseShellExecute = False
              .CreateNoWindow = False
              .RedirectStandardInput = True
              .RedirectStandardOutput = True
              .RedirectStandardError = True
              .FileName = "mon"
              .Arguments = " 10.0.0.99 4000"
          End With
          objProcess.Start()
          

          Then to write to the console keyboard stream:

          Dim kb As StreamWriter = objProcess.StandardInput
          kb.WriteLine("This is being typed in the console session...")
          

          Dave Kreskowiak Microsoft MVP - Visual Basic

          B 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Well, you got most of it right. The only stream you didn't redirect was StandardInput. That's the keyboard stream that let's you "type" something into that console session.

            Dim objProcess As New Process
            With objProcess.StartInfo
                .UseShellExecute = False
                .CreateNoWindow = False
                .RedirectStandardInput = True
                .RedirectStandardOutput = True
                .RedirectStandardError = True
                .FileName = "mon"
                .Arguments = " 10.0.0.99 4000"
            End With
            objProcess.Start()
            

            Then to write to the console keyboard stream:

            Dim kb As StreamWriter = objProcess.StandardInput
            kb.WriteLine("This is being typed in the console session...")
            

            Dave Kreskowiak Microsoft MVP - Visual Basic

            B Offline
            B Offline
            barkanb
            wrote on last edited by
            #6

            When I make the StandardInput = True objProcess.StartInfo.RedirectStandardInput = True it opens the command window but does not give any result of mon.exe. It is not enabled also. If StandardInput = False then mon.exe works fine but command window only lets input from keyboard. Should I Import any dll? B.B.

            D 1 Reply Last reply
            0
            • B barkanb

              When I make the StandardInput = True objProcess.StartInfo.RedirectStandardInput = True it opens the command window but does not give any result of mon.exe. It is not enabled also. If StandardInput = False then mon.exe works fine but command window only lets input from keyboard. Should I Import any dll? B.B.

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

              I have no idea. I've never sen anything like that. You might want to try it with a different executable. When I did something like this, it took some playing around to get all the redirect settings to work properly. You've got everything that I've used before working. If your app isn't worried about what's comming back from MON, turn off the redirection of StandardError and StandardOutput.

              Dave Kreskowiak Microsoft MVP - Visual Basic

              B 1 Reply Last reply
              0
              • D Dave Kreskowiak

                I have no idea. I've never sen anything like that. You might want to try it with a different executable. When I did something like this, it took some playing around to get all the redirect settings to work properly. You've got everything that I've used before working. If your app isn't worried about what's comming back from MON, turn off the redirection of StandardError and StandardOutput.

                Dave Kreskowiak Microsoft MVP - Visual Basic

                B Offline
                B Offline
                barkanb
                wrote on last edited by
                #8

                You are right about the code. It is working for cmd.exe or notepad.exe.. I also tested the code below tried a trick to work on cmd but second writeline command executes for cmd.exe only. I also tried SendKey.SendWait(). Do you think there is something to do with security permissions? Because when I make RedirectStandardInput = True, command window is opened but not responding(blank). That's why I am saving the debug info into a txt file. Dim objProcess As New Process With objProcess.StartInfo .UseShellExecute = False .CreateNoWindow = False .FileName = "cmd" .RedirectStandardInput = True .RedirectStandardOutput = True .RedirectStandardError = False End With objProcess.Start() Dim param(2) As String param(0) = Chr(34) & CurDir() & "/mon.exe" & Chr(34) & " 10.0.0.99 4000 > C:\a.txt" param(1) = "SET IP 10.0.0.98 > C:\b.txt" kb.WriteLine(param) kb.WriteLine(param(1)) B.B.

                B 1 Reply Last reply
                0
                • B barkanb

                  You are right about the code. It is working for cmd.exe or notepad.exe.. I also tested the code below tried a trick to work on cmd but second writeline command executes for cmd.exe only. I also tried SendKey.SendWait(). Do you think there is something to do with security permissions? Because when I make RedirectStandardInput = True, command window is opened but not responding(blank). That's why I am saving the debug info into a txt file. Dim objProcess As New Process With objProcess.StartInfo .UseShellExecute = False .CreateNoWindow = False .FileName = "cmd" .RedirectStandardInput = True .RedirectStandardOutput = True .RedirectStandardError = False End With objProcess.Start() Dim param(2) As String param(0) = Chr(34) & CurDir() & "/mon.exe" & Chr(34) & " 10.0.0.99 4000 > C:\a.txt" param(1) = "SET IP 10.0.0.98 > C:\b.txt" kb.WriteLine(param) kb.WriteLine(param(1)) B.B.

                  B Offline
                  B Offline
                  barkanb
                  wrote on last edited by
                  #9

                  Sorry for the confusion. I couldn't make it run on notepad also. Could you post me a sample code to print on notepad other than the one below. Dim objProcess As New Process With objProcess.StartInfo .UseShellExecute = False .CreateNoWindow = True .FileName = "notepad" .RedirectStandardInput = True .RedirectStandardOutput = False .RedirectStandardError = False End With objProcess.Start() Dim kb As StreamWriter = objProcess.StandardInput kb.WriteLine("A1233 ") I assume StreamWriter acts as keyboard but it is not. Maybe there is something small I am missing. I have to leave now. Appriciated for your help. Regards B.B.

                  D 1 Reply Last reply
                  0
                  • B barkanb

                    Sorry for the confusion. I couldn't make it run on notepad also. Could you post me a sample code to print on notepad other than the one below. Dim objProcess As New Process With objProcess.StartInfo .UseShellExecute = False .CreateNoWindow = True .FileName = "notepad" .RedirectStandardInput = True .RedirectStandardOutput = False .RedirectStandardError = False End With objProcess.Start() Dim kb As StreamWriter = objProcess.StandardInput kb.WriteLine("A1233 ") I assume StreamWriter acts as keyboard but it is not. Maybe there is something small I am missing. I have to leave now. Appriciated for your help. Regards B.B.

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

                    Actually, there's something large you're missing. You can't redirect Notepad because there are no streams in a Windows app to redirect. You can only do this with DOS applications that use the standard console and keyboard devices themselves, like the FTP app (FTP.EXE) that comes with Windows.

                    Dave Kreskowiak Microsoft MVP - Visual Basic

                    B 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      Actually, there's something large you're missing. You can't redirect Notepad because there are no streams in a Windows app to redirect. You can only do this with DOS applications that use the standard console and keyboard devices themselves, like the FTP app (FTP.EXE) that comes with Windows.

                      Dave Kreskowiak Microsoft MVP - Visual Basic

                      B Offline
                      B Offline
                      barkanb
                      wrote on last edited by
                      #11

                      By adding System.Environment.NewLine into writeline command, it is working now. Thanks for your help and support. :-D ' The code below enables you to run a command ' window application (exe), and send special input ' commands to that exe application. 'Execute mon.exe file by creating a new process Dim objProcess As New Process With objProcess.StartInfo .UseShellExecute = False .CreateNoWindow = True .WorkingDirectory = CurDir() .FileName = "mon.exe" .Arguments = " " & ipno & " 4000" .RedirectStandardInput = True .RedirectStandardOutput = True .RedirectStandardError = False End With objProcess.Start() Dim sIn As StreamWriter = objProcess.StandardInput Dim sOut As StreamReader = objProcess.StandardOutput Dim s as String sIn.AutoFlush = True sIn.Write("SET IP " & ipnoNEW & System.Environment.NewLine) s = sOut.ReadToEnd() objProcess.WaitForExit(5) If Not objProcess.HasExited Then objProcess.Kill() End If MsgBox(s) B.B.

                      D 1 Reply Last reply
                      0
                      • B barkanb

                        By adding System.Environment.NewLine into writeline command, it is working now. Thanks for your help and support. :-D ' The code below enables you to run a command ' window application (exe), and send special input ' commands to that exe application. 'Execute mon.exe file by creating a new process Dim objProcess As New Process With objProcess.StartInfo .UseShellExecute = False .CreateNoWindow = True .WorkingDirectory = CurDir() .FileName = "mon.exe" .Arguments = " " & ipno & " 4000" .RedirectStandardInput = True .RedirectStandardOutput = True .RedirectStandardError = False End With objProcess.Start() Dim sIn As StreamWriter = objProcess.StandardInput Dim sOut As StreamReader = objProcess.StandardOutput Dim s as String sIn.AutoFlush = True sIn.Write("SET IP " & ipnoNEW & System.Environment.NewLine) s = sOut.ReadToEnd() objProcess.WaitForExit(5) If Not objProcess.HasExited Then objProcess.Kill() End If MsgBox(s) B.B.

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

                        barkanb wrote:

                        sIn.Write("SET IP " & ipnoNEW & System.Environment.NewLine)

                        Or, you could have just used sIn.Write**Line**("SET IP" & ipnoNEW).

                        Dave Kreskowiak Microsoft MVP - Visual Basic

                        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