如何使用vb.net读取txt文件中的某一行?

时间:2013-09-12 14:27:21

标签: vb.net

我正在使用vb.net,这就是我在test.txt文件中的内容:

My name is james

I was born in Melaka

*

I like to eat fish

#

My hobby is riding

我希望我的函数在符号*和#

之间读取此文件

这是我的功能以及我将如何编辑它以将条件放入?

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
   Dim fileReader As String
   fileReader = My.Computer.FileSystem.ReadAllText("C:\Users\user\Documents\text.txt")
   txt2.Text = (fileReader)
End Sub

由于

2 个答案:

答案 0 :(得分:1)

因为你可以用LINQ做任何事情:

Dim result = From line In File.ReadLines("c:\path\to\your\file.txt")
             Skip While line <> "*"
             Skip 1
             Take While line <> "#"

For Each line In result
    ' Do something '
Next

[*]:不正确

答案 1 :(得分:1)

正则表达式是一个快速解决方案,使用(\*)([\s\S]+)(\#)模式,您可以找到*#之间的任何内容

   Dim fileReader As String
   fileReader = My.Computer.FileSystem.ReadAllText("C:\Users\user\Documents\text.txt")
   txt2.Text = System.Text.RegularExpressions.Regex.Match(fileReader,"(\*)([\s\S]+)(\#)").toString()