如何搜索文本文件并将特定文本添加到列表框 [英] How to search through a text file and add specific text to a Listbox
问题描述
好吧,我一直在疯狂尝试做这项工作。我有一个文本文件,列出帐户设置(acclist.txt),其中包含我试图提取到列表框的文本字符串。我已经尝试了所有我认识的东西,似乎无法摆脱它的高中心。我正在使用StreamReader读取文本文件,并尝试逐行处理文件,查找包含我要查找的数据的行。从这里我想我可以修剪我需要的数据并将其循环以将其添加到列表框。
我的文本文件包含多个与此类似的条目,
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,
}
Language = 1033
LoginName = {
_vs = "DOMAIN\\USERNAME\000"
}
我正在尝试将其DOMAIN和USERNAME部分提取到列表框中。这会给我一个填充DOMAIN \USERNAME条目的列表框。
到目前为止我已经开始这个了,
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,
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.
推荐答案
如果我理解正确,那么这应该可以帮助你得到你想要的东西(只需用你自己的文件名替换):
if i am understanding correctly, then this should help you get what you are wanting(just replace the filename with your own):
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
找出我的解决方案。它可能有点令人费解,但它正在发挥作用。如果有人有任何建议,我会对如何解决这个问题提出更好的建议。请找到我的以下代码。我开始将我的文本文件读入数组并过滤掉我不想看到的数据。
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.
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
这里有一个下拉程序,可让您输入参数文件名和列表框列表他们在。
程序将生成一个如下所示的列表:
DOMAIN \USERNAME
DOMAIN \USERNAME
DOMAIN \USERNAME
这就是你要找的东西?它与我上面的例子基本相同,但有一些变化。
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.
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
这篇关于如何搜索文本文件并将特定文本添加到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!