How do I read standard output from a cmd window

P

PAPutzback

My code
Dim Cmd As New Process
Dim CmdHandle As String
Dim dirResults As String
Cmd.StartInfo.FileName = "c:\working\gpg\gpg.exe"
Cmd.StartInfo.Arguments = "--yes --output :\working\inbound\test.txt
--decrypt c:\working\inbound\anstest.gpg"
Cmd.StartInfo.RedirectStandardInput = True
Cmd.StartInfo.RedirectStandardOutput = True
Cmd.StartInfo.UseShellExecute = False
Cmd.Start()

MsgBox("the name of the process", MsgBoxStyle.Information,
Cmd.ProcessName)
dirResults = Cmd.StandardOutput.ReadToEnd
MsgBox(dirResults)

The window pops up and is full of text but the dirResults string is
nothing.
Do I have to have a seperate thread to read the output?

Thanks,
Phil
 
K

Ken Tucker [MVP]

Hi,

Maybe this will help.


Dim pi As New ProcessStartInfo

Dim p As Process

pi.RedirectStandardOutput = True

pi.FileName = "c:\myconsoleapp.exe"

pi.UseShellExecute = False

Dim sw As New System.IO.StreamWriter("C:\MyOutPut.txt")

p = Process.Start(pi)

sw.WriteLine(p.StandardOutput.ReadToEnd)

p.WaitForExit()

sw.Close()



Ken

------------------------------------
My code
Dim Cmd As New Process
Dim CmdHandle As String
Dim dirResults As String
Cmd.StartInfo.FileName = "c:\working\gpg\gpg.exe"
Cmd.StartInfo.Arguments = "--yes --output :\working\inbound\test.txt
--decrypt c:\working\inbound\anstest.gpg"
Cmd.StartInfo.RedirectStandardInput = True
Cmd.StartInfo.RedirectStandardOutput = True
Cmd.StartInfo.UseShellExecute = False
Cmd.Start()

MsgBox("the name of the process", MsgBoxStyle.Information,
Cmd.ProcessName)
dirResults = Cmd.StandardOutput.ReadToEnd
MsgBox(dirResults)

The window pops up and is full of text but the dirResults string is
nothing.
Do I have to have a seperate thread to read the output?

Thanks,
Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top