Link to home
Start Free TrialLog in
Avatar of Brian
BrianFlag for United States of America

asked on

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

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?
ASKER CERTIFIED SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brian

ASKER

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

Avatar of Brian

ASKER

Thanks for the help!