在VB.NET中读取.txt文件的特定行 [英] Read specific line of a .txt file in VB.NET
本文介绍了在VB.NET中读取.txt文件的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含以下信息的文本文件:
ASKK90001BRANCH
COMPANY-HONDAHCMI
< span class =code-digit> 01
HCAISD-02
i尝试使用此代码:
Dim objReader As 新 System.IO.StreamReader(Application.StartupPath& \ SVQ .txt)
LBLcounter.Text = objReader.ReadToEnd
objReader.Close()
但它会读取所有内容,当我将ReadToEnd更改为ReadLine时,它只读取第一个。
如何阅读第3行文字? '01'?
解决方案
我发现了一个非常简单的方法。
Dim 计数器作为 字符串 = System.IO.File.ReadAllLines(Application.StartupPath& \ SQ.txt)( 2 )
LBLcounter.text = Counter
( 2)是文本行的编号。
尝试如下
Dim 行作为 字符串
使用 sr As StreamReader = 新 System.IO.StreamReader(Application.StartupPath& ; \ SQ.txt)
line = sr.ReadLine()
while (line<> Nothing )
line = sr.ReadLine()
结束 while
结束 使用
I have a text file that contains this info:
ASKK90001BRANCH
COMPANY-HONDAHCMI
01
HCAISD-02
i tried using this code:
Dim objReader As New System.IO.StreamReader(Application.StartupPath & "\SQ.txt")
LBLcounter.Text = objReader.ReadToEnd
objReader.Close()
but it reads everything and when i change 'ReadToEnd' to 'ReadLine', it only reads the first.
how do I read the 3rd line of the text? the '01'?
解决方案
I found out a very easy way.
Dim Counter As String = System.IO.File.ReadAllLines(Application.StartupPath& "\SQ.txt")(2) LBLcounter.text = Counter
the (2) is the number of the line of the text.
try like below
Dim line As String Using sr As StreamReader = New System.IO.StreamReader(Application.StartupPath & "\SQ.txt") line = sr.ReadLine() While (line <> Nothing) line = sr.ReadLine() End While End Using
这篇关于在VB.NET中读取.txt文件的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文