Home All Groups Group Topic Archive Search About

Redirecting standard output

Author
27 Sep 2006 7:23 AM
mhmtzdmr
Hi,

I want to run an application and capture its standard output. But the
following code does not generate any output. Can anyone see something
wrong?


    Public Sub RunApp(ByVal myprocess As String, ByVal param As String,
ByVal workingDir As String)

        Dim p As Process = New Process()
        Dim psi As New ProcessStartInfo()
        psi.FileName = myprocess
        psi.WorkingDirectory = workingDir
        psi.Arguments = param
        psi.UseShellExecute = False
        psi.CreateNoWindow = True
        psi.RedirectStandardOutput = True
        psi.RedirectStandardInput = True
        psi.RedirectStandardError = True
        p.StartInfo = psi
        p.Start()


        AppendLine(p.StandardOutput.ReadToEnd)

        p.WaitForExit()

    End Sub


    -----------

Then I call the sub like below.

    RunApp("c:\auunitdel.exe", "-d", "")



Thanks...

Author
27 Sep 2006 8:26 AM
Phill W.
mhmtz***@gmail.com wrote:

> I want to run an application and capture its standard output. But the
> following code does not generate any output. Can anyone see something
> wrong?
>         p.Start()
>         AppendLine(p.StandardOutput.ReadToEnd)
>         p.WaitForExit()

The program almost certainly /is/ creating some output - you're just not
waiting for the program to finish before reading the output that it
hasn't had time to create yet.

Swap the last two lines around, as in

    p.Start()
    p.WaitForExit()
    AppendLine(p.StandardOutput.ReadToEnd)

HTH,
    Phill  W.
Author
27 Sep 2006 8:48 AM
mhmtzdmr
Phill,

I have tried that but still does not work.





Phill W. yazdi:
Show quoteHide quote
> mhmtz***@gmail.com wrote:
>
> > I want to run an application and capture its standard output. But the
> > following code does not generate any output. Can anyone see something
> > wrong?
> >         p.Start()
> >         AppendLine(p.StandardOutput.ReadToEnd)
> >         p.WaitForExit()
>
> The program almost certainly /is/ creating some output - you're just not
> waiting for the program to finish before reading the output that it
> hasn't had time to create yet.
>
> Swap the last two lines around, as in
>
>     p.Start()
>     p.WaitForExit()
>     AppendLine(p.StandardOutput.ReadToEnd)
>
> HTH,
>     Phill  W.
Author
27 Sep 2006 12:33 PM
zacks
mhmtz***@gmail.com wrote:
Show quoteHide quote
> Hi,
>
> I want to run an application and capture its standard output. But the
> following code does not generate any output. Can anyone see something
> wrong?
>
>
>     Public Sub RunApp(ByVal myprocess As String, ByVal param As String,
> ByVal workingDir As String)
>
>         Dim p As Process = New Process()
>         Dim psi As New ProcessStartInfo()
>         psi.FileName = myprocess
>         psi.WorkingDirectory = workingDir
>         psi.Arguments = param
>         psi.UseShellExecute = False
>         psi.CreateNoWindow = True
>         psi.RedirectStandardOutput = True
>         psi.RedirectStandardInput = True
>         psi.RedirectStandardError = True
>         p.StartInfo = psi
>         p.Start()
>
>
>         AppendLine(p.StandardOutput.ReadToEnd)
>
>         p.WaitForExit()
>
>     End Sub
>
>
>     -----------
>
> Then I call the sub like below.
>
>     RunApp("c:\auunitdel.exe", "-d", "")

I think you will have to set up a stream reader to read the output of
the process.