Question Copy text from console window

Snooker

Member
Joined
May 20, 2010
Messages
12
Programming Experience
10+
Hello all,
I am writing a console program in vb.net 2010/.net 4.0. Through out the program there are many lines of info. presented to the user in the console window. I was wondering if there is a way to copy all those lines from the code. The reason is that I want to save the text as a log to the operations carried out.

Thanks in advance.
 
The best solution that I was able to find involved creating a class which inherited TextWriter and overriding Write and WriteLine where the new subroutine would also write to a log file.
Here is the code I came up with:

First, the imports:
VB.NET:
Imports System.IO
Imports System.Text

Next, the Writer class:
VB.NET:
Class Writer
    Inherits TextWriter 'This is the original class for the console's output
    Private mEncoding As Encoding 'Stores the encoding, passed from the original Console Output Stream
    Private mFileStream As FileStream 'Stores the FileStream to write to (the log file)
    Private mConsoleStream As Stream 'Stores the Stream to write to (so the messages still appear on the console window), this is also passed from the original Console Output Stream
    Public Sub New(ByVal encoding As Encoding, ByVal fStream As FileStream, ByVal consoleStream As Stream)
        'Constructor: takes the encoding and console stream from the original console output and the filestream to write to
        mEncoding = encoding
        mFileStream = fStream
        mConsoleStream = consoleStream
    End Sub
    Public Overrides ReadOnly Property Encoding As Encoding
        'Required, as Encoding is declared as MustOverride in the TextWriter class
        Get
            Return mEncoding 'Returns the encoding we took from the original console stream
        End Get
    End Property
    Public Overrides Sub WriteLine(ByVal value As String)
        'Here we are overriding the WriteLine function. Basically, we are just adding a new line to the end of the message and calling the Write function
        value &= vbNewLine
        Write(value)
    End Sub
    Public Overrides Sub Write(ByVal value As String)
        'Here is where we actually write to both streams
        Dim bytes As Byte() = Encoding.GetBytes(value) 'First we need to get the bytes to write, we do this by calling the GetBytes function from the encoding and passing the string
        mFileStream.Write(bytes, 0, bytes.Length) 'Write to the log file
        mConsoleStream.Write(bytes, 0, bytes.Length) 'Write to the console window
    End Sub
End Class

Finally, the usage:
VB.NET:
Dim path As String = "C:\log.txt" 'Put whatever log path you want, this is just the path of the file that we will be writing to
Dim fStream As New FileStream(path, FileMode.OpenOrCreate, FileAccess.Write) 'This creates a filestream which our Writer class will use to write to the file
Dim writer As New Writer(Console.Out.Encoding, fStream, Console.OpenStandardOutput()) 'Create an instance of our Writer class which will take the original encoding, the file stream we created on the line above, and the regular output method for the console (to write to the console window)
Console.SetOut(writer) 'This actually sets the output of the console. Now the functions "Console.WriteLine" and "Console.Write" will write to both the console and the file.
 
Dude.. you want to write it all to a text file ? Now I understand that up there is the proper way to do it, but wow. It would be so much easier to do this:

dim Writer as new streamwriter("C:\log.txt")
Dim x as string
x = "Hello"
Console.writeline(X)
writer.write(x)

Jobs a gooden!! I just dont believe in massive amounts of code. If needs be you could even write a sub routine for it or a module to avoid repetition of code :)
 
Thanks Christopherx, this is more or less what I did already... :) I created a sub called LOG and passed the log file name and the string to it.. pretty straight forward. What I was hoping for though is something copies the whole text at the end and dump it to the log file..! Maybe in VS coming release they will put such a function :)

Thanks to everyone for the help... much appreciated.
Cheers
 
Hmmm.. Well, now this is iffy. You could try and get the streamreader to read what's going on in the cmd box, save it to a string variable and write all that to a text file. It may end up being all on one line however, let me give it a shot. I'll post the source code on here if I can get it working :)
 
Failed! Gives a load of stuff about the programme! Another work around, would be to do this in a windows form app. If you need help, pm me :)
 

Latest posts

Back
Top