Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Alright I have been going crazy trying to make this work. I have a text file thats lists account setups(acclist.txt) that has strings of text I am trying to extract to list box. I have tried everything I know and can't seem to get off of high center with it. I am reading the text file with a StreamReader and trying to process the file line by line looking for the line that contains the data I am looking for. From here I figured I could trim the data I need and loop it through to add it to a listbox.

My text file contains multiple entries similar to this,

C#
}
Language = 1033
LoginName = {
  _vs = "DOMAIN\\USERNAME\000"
}


I am trying to extract the DOMAIN and USERNAME portion of this to a list box. This would give me a list box filled with DOMAIN\USERNAME entries.

I have started this so far,

VB
Sub ReadAccList()
    Dim reader As StreamReader = New StreamReader("acclist.txt")
    Dim line = reader.ReadToEnd.Split(vbNewLine)
    lstCurrentUsers.Items.AddRange(line)
    For i As Integer = lstCurrentUsers.Items.Count - 1 To 0 Step -1
        If Not lstCurrentUsers.Items(i).Contains("    _vs = ") Then
            lstCurrentUsers.Items.RemoveAt(i)
        End If
    Next
End Sub


Does anyone have an example of how I can get this working. It seems elementary in some way and I just can't seem to get it.
Posted
Updated 14-Nov-15 15:03pm
v2

if i am understanding correctly, then this should help you get what you are wanting(just replace the filename with your own):

VB
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim sr As IO.StreamReader = New IO.StreamReader("c:\tester.txt")
    While Not sr.EndOfStream
        Dim s As String = sr.ReadLine()
        s = LTrim(s)
        s = RTrim(s)
        If Strings.Left(s, 3) = LCase("_vs") Then
            Dim arr() As String = Strings.Split(s, " = ")
            s = arr(1)
            s = Strings.Replace(s, """", "")

            arr = Strings.Split(s, "\\")
            Dim domain As String = arr(0)
            Dim username As String = arr(1)

            arr = Split(username, "\")
            username = arr(0)

            ListBox1.Items.Add(domain & " - " & username)
        End If
    End While
    sr.Close()
    sr.Dispose()
End Sub
 
Share this answer
 
v2
Comments
CrashXP 14-Nov-15 21:38pm    
That really helps, forgot all about using EndOfStream. It does pose a new challenge I didn't notice before. I have variables in this text file that have ( _vs = ""). I managed to figure out how to filter my listbox contents down to the valid DOMAIN\USER Accounts by using the following code

Dim reader As StreamReader = New StreamReader("acclist.txt")
Dim line = reader.ReadToEnd.Split(vbNewLine)
lstCurrentUsers.Items.AddRange(line)
For i As Integer = lstCurrentUsers.Items.Count - 1 To 0 Step -1
If Not lstCurrentUsers.Items(i).Contains("\") Then
lstCurrentUsers.Items.RemoveAt(i)
End If
Next

However my listbox populates with all the entries like ( _vs = "Domain\Username\000") which is almost exactly what I am working toward. I think if I can trim the last 4 characters off (000") and the first and the 11 characters off ( _vs = ") then I can replace the (\) and I have it made. I am going to play with the example you provided and see if I can massage the output a bit more.

Thanks for the direction, I will report back when I can come up with a final version.
Figured out my solution. It may be a tad convoluted but it's working. I'd be open to better suggestions on how to solve this in the future if anyone has any. Please find my below code. I started by reading my text file into an array and filtering out the data I do not want to see.

VB
Sub Acclist()
    Dim path As String = "acclist.txt"
    Dim StringArrayOfTextLines() As String = System.IO.File.ReadAllLines(path)
    Dim FrontTrimmedText As String
    Dim DoubleSlashFixed As String
    Dim RearTrimmedText As String
    For Each s In StringArrayOfTextLines
        If s.Contains("\\") Then
            FrontTrimmedText = s.Replace("    _vs = """, "")
            RearTrimmedText = FrontTrimmedText.Replace("\000""", "")
            DoubleSlashFixed = RearTrimmedText.Replace("\\", "\")
            lstCurrentUsers.Items.Add(DoubleSlashFixed)
        End If
    Next
End Sub
 
Share this answer
 
v2
here is a drop in procedure that lets you enter the filename into the parameters along with the listbox to list them in.
the procedure will produce a list that looks as following:

DOMAIN\USERNAME
DOMAIN\USERNAME
DOMAIN\USERNAME

is this what you are looking for? It is basically the same as my example above, but with a few changes.

VB
Public Sub LoadAccountList(ByVal filename As String, ByRef lstBox As ListBox)
    If lstBox Is Nothing Then Return
    If filename = "" Then Return
    Dim fNameExist As Boolean = FileIO.FileSystem.FileExists(filename)
    If Not fNameExist Then Return

    Dim filereader As IO.StreamReader = New IO.StreamReader(filename)
    While Not filereader.EndOfStream
        Dim curLine As String = filereader.ReadLine.Trim
        If curLine.Contains(LCase("_vs")) Then
            Dim arr() As String = Strings.Split(curLine, " = ")
            curLine = arr(1)
            curLine = curLine.Replace("""", "")
            If curLine <> "" Then
                curLine = curLine.Replace("\\", "\")
                curLine = Strings.Left(curLine, curLine.Length - 4)
                lstBox.Items.Add(curLine)
            End If
        End If
    End While
    filereader.Close()
    filereader.Dispose()
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900