搜索文本文件并返回一行文本

时间:2016-10-14 15:17:48

标签: vb.net do-while

我正在尝试创建一个程序,它将在文本文件中搜索一行文本,然后返回完整的信息行。

示例行:Joe Blogs JBL 1234

搜索:Joe博客

搜索返回:Joe Blogs JBL 1234

为了使它尽可能简单,我有2个文本框和& 1个按钮。

Textbox1 =搜索

Textbox2 =搜索结果

按钮=搜索按钮

任何人都可以告诉我该怎么做,因为我发现它真的很难。我是VB编码的新手,所以最简单的代码会有所帮助!

这是我到目前为止所做的:

Imports System.IO

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    ' Input Text Error 

    If TextBox1.TextLength = 0 Then
        MsgBox("Please enter a Staff Name or Staff Code", MsgBoxStyle.Information, "Error")
    End If

    'Perform Search

    Dim strText As String = SearchFile("F:\Documents\Documents\Visual Studio 2015\Projects\ExtentionLocator\ExtentionLocator\Extentionlist.txt", TextBox1.Text)
    If strText <> String.Empty Then
        TextBox2.Text = strText
    End If

End Sub

'Search Function

Public Shared Function SearchFile(ByVal strFilePath As String, ByVal strSearchTerm As String) As String
    Dim sr As StreamReader = New StreamReader(strFilePath)
    Dim strLine As String = String.Empty

    Try
        Do While sr.Peek() >= 0
            strLine = String.Empty
            strLine = sr.ReadLine
            If strLine.Contains(strSearchTerm) Then
                sr.Close()
                Exit Do
            End If
        Loop

        Return strLine
    Catch ex As Exception
        Return String.Empty
    End Try
End Function

'Clear Button

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    TextBox2.Text = ""
    TextBox1.Text = ""
End Sub

' Open The text file 

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Process.Start("C:\Users\kylesnelling\Documents\Visual Studio 2015\Projects\ExtentionLocator\ExtentionLocator\Extentionlist.txt")
End Sub

End Class

每当我执行搜索时,我回来的都是文本文件的最后一行...有谁知道为什么?

2 个答案:

答案 0 :(得分:0)

正如Alex B在评论中所述,行If strLine.Contains("textbox1.text")应为If strLine.Contains(strSearchTerm)

此外,您没有将搜索项传递给该功能。您在字符串textbox1.text中传递的下面一行是字符串,而不是文本框中的文本。因此,为什么你永远找不到你要搜索的行,并且总是返回文件的最后一条记录。

Dim strText As String = SearchFile("F:\Documents\Documents\Visual Studio 2015\Projects\ExtentionLocator\ExtentionLocator\Extentionlist.txt", "textbox1,text")

这一行应该是:

Dim strText As String = SearchFile("F:\Documents\Documents\Visual Studio 2015\Projects\ExtentionLocator\ExtentionLocator\Extentionlist.txt", textbox1.text)

同样使用此行Dim sr As StreamReader = New StreamReader("F:\Documents\Documents\Visual Studio 2015\Projects\ExtentionLocator\ExtentionLocator\Extentionlist.txt"),当您已在变量strFilePath中传入此文件位置时,为什么使用了相同的路径目标。

行应为Dim sr As StreamReader = New StreamReader(strFilePath)

最好使用传递给函数的变量,否则此函数对可能引用它的其他代码部分或搜索术语或文件路径发生变化时不会非常有用。

从评论更新: strLine.ToUpper.Contains(strSearchTerm.ToUpper)此行将使文本大写和您要搜索的单词为大写,这将允许它们忽略区分大小写,因此例如,“text”可以与“Text”匹配,两者都被转换为“ TEXT“用于比较时。

答案 1 :(得分:0)

给这个尝试的朋友。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim searchResult As String = SearchFile("F:\Documents\Documents\Visual Studio 2015\Projects\ExtentionLocator\ExtentionLocator\Extentionlist.txt", _
                                            "text to search for")

    Me.TextBox2.Text = searchResult

End Sub

Public Shared Function SearchFile(ByVal strFilePath As String, ByVal strSearchTerm As String) As String


    Dim fs As New System.IO.StreamReader(strFilePath)

    Dim currentLine As String = String.Empty

    Try
        Dim searchResult As String = String.Empty


        Do While fs.EndOfStream = False
            currentLine = fs.ReadLine
            If currentLine.IndexOf(strSearchTerm) > -1 Then
                searchResult = currentLine
                Exit Do
            End If
        Loop


        Return searchResult

    Catch ex As Exception
        Return String.Empty
    End Try
End Function