How to copy a StreamReader to a List(of string) in VB.net

Brian
Brian used Ask the Experts™
on
Is there a way to copy a StreamReader to a List without using a loop? I have the following code where I copy it to a textbox, but I would like to copy it to a List to remove some of the lines before outputting it to my textbox.
Dim SR As System.IO.StreamReader = CMDprocess.StandardOutput
'run some commandline stuff
OutputTextBox.AppendText(SR.ReadToEnd) 'this works and outputs the lines of text to the textbox
'I'd like to do something like this
        Dim strOutput As List(Of String)
        strOutput.Add(SR.ReadToEnd) 'this does not work

Open in new window

Is there a way I can copy the contents of SR to strOutput without using a loop?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014
Commented:
A List(of) is essentially an array, so you'd have to have some sort of delimiter to split the string into an array. If you do (perhaps a comma?) you could do something like this:

Dim yourList as List(of String) = SR.ReadToEnd.ToString.Split(",")

I haven't tried this, but assuming you could split the string correctly it should work.
BrianSystems Administrator

Author

Commented:
So I tried Dim strOutput As List(Of String) = SR.ReadToEnd.ToString.Split(","), but that did not work. After thinking about it, I realized that I'm wasting my time putting this into an array.  I just want to step thu the StreamReader object and output the lines to the textbox one by one as long as they don't contain certain things.  Here's what I'm looking to do...

1. Don't start outputting what is in SR until I reach a line that contains "Port      Description".
2. Keep outputting each line to the textbox until a line is reached that contains "Oob  Type".

I have the following code that outputs what I want, but maybe someone can give me some tips on how to clean it up a little and make it more concise. I'm not a professional programmer, just a dabbler, so I'm sure the code could be done better. Thanks!
        Dim strLine As String = ""                              '// Current line of text
        Dim boolStart As Boolean = False                        '// Start output when true
        Do While (Not strLine.Contains("Oob  Type"))            '// Do until a certain line is reached
            strLine = SR.ReadLine                               '// Read in line
            If strLine.Contains("Port      Description") Then   '// Check if start has been reached
                boolStart = True                                '// Set true to start output
            End If
            '// Output line when it contains useful info
            If boolStart = True And Not strLine.Contains("Oob  Type") Then
                OutputTextBox.AppendText(strLine)
                OutputTextBox.AppendText(vbCrLf)
            End If
        Loop
        SR.Close()

Open in new window

BrianSystems Administrator

Author

Commented:
Thanks for the help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial