如何在vb.net中读取某行的txt文件? [英] How to read txt file for a certain line in vb.net ?

查看:164
本文介绍了如何在vb.net中读取某行的txt文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的文件txt文件中有这个:





abcdef

##

hijkl

##

mnop



Dim fileReader As String

fileReader = My.Computer.FileSystem.ReadAllText(C:\ Users.test.txt)

txtStrip.Text = System.Text.RegularExpressions.Regex.Match(fileReader, (\#)([\\\\ S] +)(\ ##))。ToString()



我想在我的代码中执行介于#和第一个##之间,只有= abdef



我将如何编辑该代码?我正在使用vb.net

I have this in my file txt file:

#
abcdef
##
hijkl
##
mnop

Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\Users.test.txt")
txtStrip.Text = System.Text.RegularExpressions.Regex.Match(fileReader,(\#)([\s\S]+)(\##)).ToString()

I want to execute in my code is between # and the first ## which is = abdef only

How I'm going to edit that code ? I'm using vb.net

推荐答案

由于您的输入文件看起来很小,您可以执行以下操作:使用 System.IO.File .ReadAllLines (而不是 ReadAllText 。这个方法返回一个字符串数组,每个字符串代表一条线。通过索引和使用来访问数组元素你需要的线路。



请参阅: http://msdn.microsoft.com/en-us/library/system.io.file.readalllines.aspx [ ^ ]。







As your input files are apparently small, you can do the following: use System.IO.File.ReadAllLines (instead of ReadAllText. This method returns your an array of strings each representing, surprisingly, a line. Access array elements by index and use the line(s) you need.

Please see: http://msdn.microsoft.com/en-us/library/system.io.file.readalllines.aspx[^].



gogole_yuna问:
gogole_yuna asked:

如果我的话怎么办? txt文件很大..?

what if my txt file is big..?

然后你只需使用 System.IO.StreamReader ,方法 ReadLine 。在阅读时计算行数。选择您需要的行。请参阅:

http://msdn.microsoft.com /en-us/library/system.io.streamreader.aspx [ ^ ]。



-SA

Then you would simply use System.IO.StreamReader with the method ReadLine. Count lines as you read. Pick the one(s) you need. Please see:
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx[^].

—SA


这篇关于如何在vb.net中读取某行的txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆