如何在vb.net中从txt文件中获取第一行
这是我读取TXT文件并将其放入datagridview的代码如何在vb.net中从txt文件中获取第一行,vb.net,text-files,Vb.net,Text Files,这是我读取TXT文件并将其放入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
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
现在我只想获取TXT文件的第一条记录并将其放入msgbox中,我如何才能做到这一点
我试过这个:
MsgBox(SplitLine.tostring)
但此代码的输出如下:System.String[]
谢谢。我已经为这一个做了一个工作代码,在这里:
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
将msgboxReader作为新System.IO.StreamReader(文件名)进行调整
msgbox(msgboxReader.ReadLine())
Dim objReader作为新System.IO.StreamReader(文件名)
Do While objReader.Peek()-1
TextLine=objReader.ReadLine()
拆分行=拆分(文本行,“,”)
dvList.Rows.Add(拆分行)
环
更新
我只是声明另一个读者只阅读第一行
首先变暗为布尔值=真
按如下方式编辑循环:
Do While objReader.Peek() <> -1
TextLine = objReader.ReadLine()
If First Then MessageBox(TextLine) : First = False
SplitLine = Split(TextLine, ",")
dvList.Rows.Add(SplitLine)
Loop
Do While objReader.Peek()-1
TextLine=objReader.ReadLine()
If First Then MessageBox(文本行):First=False
拆分行=拆分(文本行,“,”)
dvList.Rows.Add(拆分行)
环
您可以不使用两个单独的读卡器,因为您已经有了可用的值
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
Dim objReader作为新System.IO.StreamReader(文件名)
文件中迄今为止读取的作为整数的Dim LINECUNT行
Do While objReader.Peek()-1
TextLine=objReader.ReadLine()
如果lineCount=0,则msgbox(TextLine)'将在第一次迭代中显示msgbox
拆分行=拆分(文本行,“,”)
dvList.Rows.Add(拆分行)
lineCount=lineCount+1'增量lineCount
环
这段代码对我来说很好用(但可能会因为长文件而变慢)
第一次在你的循环中,
TextLine
已经包含了第一条记录。@Darkthulhu:谢谢你指出这一点,我想我可以使用splitline,我已经为此编写了一个工作代码,再次感谢你。一旦你通过调用该命令将读卡器提前一行,第一条记录将不会在do..while()中被解析.我想你不明白<如果在循环外调用ReadLine(),code>dvList将不包含第一条记录。@Darkthulhu:您好,先生,我已经更新了我的问题,您能告诉我这是否正确吗?因为在我的代码中,它现在可以工作了,谢谢您的帮助。这是正确的@Matthew。但不需要另一个读者。看到我的答案了。啊,我明白了,这段代码也适用于我,谢谢你。老兄,你还使用VB6吗?:)不,先生,我是vb.net,为什么?是因为msgbox吗?:)啊,好的,谢谢:)我是vb.net的新手,我现在就用这个啊,好的,先生,这就是我要找的,谢谢你:)
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
'' fileToOpen is the var with address of file (E.g.: c:\txt.txt)
Dim lineOneFromFile As String = IO.File.ReadAllLines(fileToOpen)(0)