我正在做一个基于用户在文本框中提供的某些单词来搜索文档以搜索文本文件的项目。 我在基本的Windows窗体应用程序上使用Visual Studio 2013,并希望基于文本框中的单词打开文件组件。 文本必须从所需的位置打开。
答案 0 :(得分:0)
如果我已经理解了您的问题,那么您希望根据用户输入在txt文件中找到一个关键字,以便进行某些操作。这是一个小片段,为您提供了一些示例和一个良好的起点。
代码段
Dim reader As StreamReader
Dim txtInput as String = "YOUR_TXT_FILE_PATH"
'The reader opens the txt input specified with the file path
reader = My.Computer.FileSystem.OpenTextFileReader(txtInput)
'Create a string from the txt file
Dim txtString as String = reader.ReadLine.TrimStart
Dim wordFound as String
'Use Regex inside a Try/Catch statement in order to catch any possible exception.
'If Regex.Match doesn't find your TextBox1.Text inside the string it will throw an exception
Try
'Use Regex to find your word or your expression inside the string created before
For Each data As Match In Regex.Matches(txtString, TextBox1.Text)
wordFound = txtString.Substring(data.index, TextBox1.Text.Lenght())
'Do your stuff with your word found in the txt...
Next
Catch ex As Exception
Err.Clear()
Finally
'Close the reader before the Try/Catch statement
reader.Close()
End Try
我建议您首先阅读有关Regex.Match的文档,如果您有任何疑问,请随时提问。
欢迎使用StackOverflow!