【问题标题】:Search through text file and return a line of text搜索文本文件并返回一行文本
【发布时间】:2016-10-14 15:17:48
【问题描述】:

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

示例行: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

每当我执行搜索时,我得到的只是文本文件的最后一行...有人知道为什么吗?

【问题讨论】:

  • If strLine.Contains(strSearchTerm) ? .此外,您应该将您的 StreamReader 包含在 Using 块中,以确保正确处理。
  • 加:Dim strText As String = SearchFile("F:\Documents\Documents\Visual Studio 2015\Projects\ExtentionLocator\ExtentionLocator\Extentionlist.txt", Textbox1.Text) 不带双引号。
  • 如何将流阅读器封装到块中?我已经进行了您建议的更改,但是每次我搜索某些内容时,它只会显示文档的最后一行,实际上并没有执行搜索
  • 你能用你当前的代码编辑你的问题吗?看看这个例子如何使用Using
  • 一切都为你完成:)

标签: vb.net do-while


【解决方案1】:

正如 Alex B 在 cmets 中所说,行 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)

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

从 cmets 更新: strLine.ToUpper.Contains(strSearchTerm.ToUpper) 这一行将把文本和你要搜索的单词都变成大写,这将允许他们忽略大小写,例如,“text”可以通过将两者都转换为“TEXT”来匹配“Text”用来比较的时候。

【讨论】:

  • 用 alex B 的代码重新更新,实现了你已经推荐的两个改变,感谢到目前为止的帮助:D
  • 钙 我不太确定我到底在做什么 我之前在 VB 中做过一些事情,但我现在正在努力学习它 我还没有完全理解一切到目前为止,有谁知道如何让它真正搜索文件并返回我想要的数据?
  • 你改了它现在在做什么?
  • 无论我输入什么搜索条件,结果都将始终是 .txt 文档的最后一行。如果我在 .txt 文档中添加新行并保存它,搜索将始终显示最后一行。
  • 我会假设,要么它可能是一些事情,我会尝试通过代码来测试这一点。 1. 你的循环真的正确循环了吗? 2. 搜索词实际上是在文本中吗?并以相同的文本格式,例如相同的大小写?
【解决方案2】:

朋友可以试试看。

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

【讨论】:

    猜你喜欢
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    相关资源
    最近更新 更多