Capturing std out from a process

  • Thread starter Thread starter iforsyth
  • Start date Start date
I

iforsyth

Hi.

Running an app with System.Diagnostics.Process process with:

Sample code:

myproc = New System.Diagnostics.Process()
strPgm = "myapp.exe"
strArgs = ""
myproc.StartInfo = New
System.Diagnostics.ProcessStartInfo(strPgm, strArgs)
myproc.Start()
myproc.WaitForExit()
myproc.Close()


At points in the application, I write standard out to a file, i.e.:

Dim objLogFS As FileStream = New
FileStream(System.AppDomain.CurrentDomain.BaseDirectory() & "/Log/" &
Format(Now(), "yyyyMMddhhmmss") & ".txt", FileMode.Create)
Dim objLogStreamWriter As StreamWriter = New
StreamWriter(objLogFS)
Console.SetOut(objLogStreamWriter)
Console.WriteLine(vbCrLf & "***Application Ended At " & Now())

But this only captures output within the main application, not the
standard output that's being generated by the process. How do I get
the output from myproc and write it to a file?

Ian
 
Hi Lan,

Thanks for posting in the community. My name is Peter, and I will be
assisting you on this issue.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to capture the spawned
process's standard output.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

Here I write a sample for you, you may have a try and let me know the
result.
Imports System.Diagnostics
imports System.IO
Module Module1
Sub Main()
Dim myproc As Process
myproc = New System.Diagnostics.Process
Dim strPgm As String = "myapp.exe"
Dim strArgs As String = ""
myproc.StartInfo.FileName = strPgm
myproc.StartInfo.Arguments = strArgs
myproc.StartInfo.UseShellExecute = False
myproc.StartInfo.RedirectStandardOutput = True
myproc.StartInfo.RedirectStandardError = True
myproc.StartInfo.CreateNoWindow = True
myproc.Start()
Dim output As String = myproc.StandardOutput.ReadToEnd()
Dim objLogStreamWriter As StreamWriter = New
StreamWriter("c:\test.txt")
objLogStreamWriter.Write(output)
objLogStreamWriter.Close()
myproc.WaitForExit()
myproc.Close()
Console.WriteLine(output)
End Sub
End Module

Launching a process and displaying its standard output
http://www.codeproject.com/csharp/LaunchProcess.asp

If you have any concern on this issue, please post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Lan,

Did Herfried and my suggestion help you?
If you have any conern on this issue, please post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top