Read Command Line Output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    Read Command Line Output

    How can I read the output from a command line utility such as ping,
    nslookup, etc, from within my app??? I know that it is possible to
    output the results to a text file using the > function in a command
    window and then read the file, but doing this for 1000+ files will
    cause a lot of unnecessary read/writes to the disc and affect
    performance...

    I am not asking for a .NET way to ping or do a DNS lookup, I am
    interested in the concept of reading the output from a command line
    utility...

    Thanks
  • Tom Shelton

    #2
    Re: Read Command Line Output

    In article <7a0a0475.04052 50703.47fae444@ posting.google. com>, Steve wrote:[color=blue]
    > How can I read the output from a command line utility such as ping,
    > nslookup, etc, from within my app??? I know that it is possible to
    > output the results to a text file using the > function in a command
    > window and then read the file, but doing this for 1000+ files will
    > cause a lot of unnecessary read/writes to the disc and affect
    > performance...
    >
    > I am not asking for a .NET way to ping or do a DNS lookup, I am
    > interested in the concept of reading the output from a command line
    > utility...
    >
    > Thanks[/color]

    You use System.Diagnost ics.Process to start the application, and then
    you redirect it's standard output by making sure the startinfo objects'
    useshellexecute is set to false and the redirectstandar doutput is set to
    true. Then you can use the processes StandardOutput property to read
    the text....

    --
    Tom Shelton [MVP]

    Comment

    • Cor Ligthert

      #3
      Re: Read Command Line Output

      Hi Steve,

      Roughly copied from a program and changed

      I hope this helps?

      Cor


      \\\
      Dim p As New Process
      p.StartInfo.Use ShellExecute = False
      p.StartInfo.Red irectStandardOu tput = True
      p.StartInfo.Arg uments = myargumentstrin g
      p.StartInfo.Wor kingDirectory = myworkdirectory string
      p.StartInfo.Fil eName =C:\myprogram
      p.Start()
      Dim sr As IO.StreamReader = p.StandardOutpu t
      Dim sb As New System.Text.Str ingBuilder("")
      Dim input As Integer = sr.Read
      Do Until input = -1
      sb.Append(ChrW( input))
      input = sr.Read
      Loop
      ///


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Read Command Line Output

        * google@valleybo y.net (Steve) scripsit:[color=blue]
        > How can I read the output from a command line utility such as ping,
        > nslookup, etc, from within my app??? I know that it is possible to
        > output the results to a text file using the > function in a command
        > window and then read the file, but doing this for 1000+ files will
        > cause a lot of unnecessary read/writes to the disc and affect
        > performance...[/color]

        <URL:http://dotnet.mvps.org/dotnet/samples/miscsamples/downloads/RedirectConsole .zip>

        --
        Herfried K. Wagner [MVP]
        <URL:http://dotnet.mvps.org/>

        Comment

        • Steve

          #5
          Re: Read Command Line Output

          Perfect, thanks... I have created a function if anyone is interested
          (watch for line breaks)...

          Friend Function ReadCmdOutput(B yVal applicationName As String,
          Optional ByVal applicationArgs As String = "", Optional ByVal
          workingDirector y As String = "", Optional ByVal showWindow As Boolean
          = False) As String
          Try
          Dim processObj As New Process

          processObj.Star tInfo.UseShellE xecute = False
          processObj.Star tInfo.RedirectS tandardOutput = True
          processObj.Star tInfo.FileName = applicationName
          processObj.Star tInfo.Arguments = applicationArgs
          processObj.Star tInfo.WorkingDi rectory = workingDirector y

          If showWindow = True Then
          processObj.Star tInfo.CreateNoW indow = False
          Else
          processObj.Star tInfo.CreateNoW indow = True
          End If

          processObj.Star t()
          processObj.Wait ForExit()

          Return processObj.Stan dardOutput.Read ToEnd
          Catch ex As Exception
          Return ""
          End Try
          End Function
          "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message news:<eRoAgymQE HA.2468@tk2msft ngp13.phx.gbl>. ..[color=blue]
          > Hi Steve,
          >
          > Roughly copied from a program and changed
          >
          > I hope this helps?
          >
          > Cor
          >
          >
          > \\\
          > Dim p As New Process
          > p.StartInfo.Use ShellExecute = False
          > p.StartInfo.Red irectStandardOu tput = True
          > p.StartInfo.Arg uments = myargumentstrin g
          > p.StartInfo.Wor kingDirectory = myworkdirectory string
          > p.StartInfo.Fil eName =C:\myprogram
          > p.Start()
          > Dim sr As IO.StreamReader = p.StandardOutpu t
          > Dim sb As New System.Text.Str ingBuilder("")
          > Dim input As Integer = sr.Read
          > Do Until input = -1
          > sb.Append(ChrW( input))
          > input = sr.Read
          > Loop
          > ///[/color]

          Comment

          Working...