vb.net 如何从vb.net中的txt文件中获取第一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18886650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the first row from the txt File in vb.net
提问by Matthew
This is my code for reading the txtfile and put it in the datagridview
这是我读取txtfile并将其放入datagridview的代码
Dim filename As String = String.Empty
Dim TextLine As String = ""
Dim SplitLine() As String
ofd1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
ofd1.FilterIndex = 2
ofd1.RestoreDirectory = True
ofd1.Title = "Open Text File"
'get the filename of the txt file
If ofd1.ShowDialog() = DialogResult.OK Then
filename = ofd1.FileName
End If
'if the filename is existing
If System.IO.File.Exists(filename) = True Then
Dim objReader As New System.IO.StreamReader(filename)
Do While objReader.Peek() <> -1
TextLine = objReader.ReadLine()
SplitLine = Split(TextLine, ",")
dvList.Rows.Add(SplitLine)
Loop
End If
This is the txt File:
这是txt文件:
False, 1-305-9097-01-2, 879.75, 122009, fr
False, 1-305-9097-02-2, 879.75, 122009, fr
False, 1-305-9097-02-3, 879.75, 122009, fr
False, 1-305-9097-03-5, 899.75, 122009, fr
Now I want to get only the first record of my txtfile and put it in the msgbox, How can I do this?
现在我只想获取我的 txtfile 的第一条记录并将其放入 msgbox,我该怎么做?
I tried this:
我试过这个:
MsgBox(SplitLine.tostring)
But the output of this code is this: System.String[]
但是这段代码的输出是这样的:System.String[]
Thank you.
谢谢你。
回答by Maher
Dim First as Boolean = True
Dim First 为 Boolean = True
Edit the loop like this:
像这样编辑循环:
Do While objReader.Peek() <> -1
TextLine = objReader.ReadLine()
If First Then MessageBox(TextLine) : First = False
SplitLine = Split(TextLine, ",")
dvList.Rows.Add(SplitLine)
Loop
回答by Anirudh Ramanathan
You can do it without 2 separate readers, since you already have the value available.
您可以在没有 2 个单独阅读器的情况下完成此操作,因为您已经拥有可用价值。
Dim objReader As New System.IO.StreamReader(filename)
Dim lineCount as Integer 'lines read so far in file
Do While objReader.Peek() <> -1
TextLine = objReader.ReadLine()
If lineCount = 0 Then msgbox(TextLine) 'will show msgbox in first iteration
SplitLine = Split(TextLine, ",")
dvList.Rows.Add(SplitLine)
lineCount = lineCount + 1 'increment lineCount
Loop
回答by Sergio P. H.
This piece of code work just fine for me (but maybe, going to be slow with long files).
这段代码对我来说工作得很好(但也许,长文件会很慢)。
'' fileToOpen is the var with address of file (E.g.: c:\txt.txt)
Dim lineOneFromFile As String = IO.File.ReadAllLines(fileToOpen)(0)
回答by Matthew
I have already made a working code for this one, here it is:
我已经为此编写了一个工作代码,这里是:
Dim msgboxReader As New System.IO.StreamReader(filename)
msgbox(msgboxReader.ReadLine())
Dim objReader As New System.IO.StreamReader(filename)
Do While objReader.Peek() <> -1
TextLine = objReader.ReadLine()
SplitLine = Split(TextLine, ",")
dvList.Rows.Add(SplitLine)
Loop
UPDATEI just declare another reader to read the first line only
更新我只是声明另一个读者只阅读第一行

