Question Reading Console output in a window?

VentureFree

Well-known member
Joined
Jan 9, 2008
Messages
54
Programming Experience
5-10
I've got an old dynamic link library that I'm using which was created primarily to be used in a console environment. As such it generally indicates what it's doing through Console.Write(). I've recently created a windows form which is using some of the functions from this library. I'd like to be able to grab the console output and place it in a textbox on my windows form so that the user can have some indication of the progress (right now it just says "Working"). Is this possible, and if so how would one go about doing that? Thanks.
 
With Console.SetOut you can set a TextWriter that will write everything the process writes to Console.

This can for example be a class that inherits StringWriter and overrides the Write/WriteLine(string) method, with such a class you can detect when a string is written and raise an event instead of polling the data cache for changes.

Just to show the concept I'll provide short example, this just use a StringBuilder cache that is read after something was written to console:
VB.NET:
Dim builder As New System.Text.StringBuilder
Dim w As New IO.StringWriter(builder)
Console.SetOut(w)
Dim dll As New ClassLibrary1.Class1
dll.ConsoleWrite("something")
Dim s As String = builder.ToString
 
Another approach:
VB.NET:
      Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
              Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
              CMDThread.Start()
      End Sub

      Private Sub CMDAutomate()
              Dim myprocess As New Process
              Dim StartInfo As New System.Diagnostics.ProcessStartInfo
              StartInfo.FileName = "cmd" 'starts cmd window
              StartInfo.RedirectStandardInput = True
              StartInfo.RedirectStandardOutput = True
              StartInfo.UseShellExecute = False 'required to redirect
              myprocess.StartInfo = StartInfo
              myprocess.Start()
              Dim SR As System.IO.StreamReader = myprocess.StandardOutput
              Dim SW As System.IO.StreamWriter = myprocess.StandardInput
              SW.WriteLine(txtCommand.Text) 'the command you wish to run.....
              SW.WriteLine("exit") 'exits command prompt window
              txtResults.Text = SR.ReadToEnd 'returns results of the command window
              SW.Close()
              SR.Close()
      End Sub
[ame=http://www.vbforums.com/showthread.php?t=381405]Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005] - VBForums[/ame]
 
JB, VentureFree uses a dll (that writes to standard out) in current process (windows app), he's not starting another process.
 
Back
Top