基础有关问题:关于读取文本文件
基础问题:关于读取文本文件
小弟业余爱好自己写点小程序自用,以前用VB6,现在想学学.net
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
With OpenFileDialog2
.Filter = "桩号文件(.txt)|*.txt"
.FilterIndex = 1
.ShowDialog()
End With
Dim ZhhaoFile As String
ZhhaoFile = OpenFileDialog2.FileName
Dim objFile As New System.IO.StringReader(ZhhaoFile)
Dim StrContents As String
StrContents = objFile.ReadToEnd()
objFile.Close()
objFile.Dispose()
TextBox1.Text = StrContents
End Sub
请问这段代码,最后的textbox1里面得到的为什么是路径?譬如"c:\text.txt"之类,而不是文件内容。
要怎样才能读取文本文件内容?
谢谢!
------解决方案--------------------
不应该用StringReader而应该用StreamReader,StringReader会把一个字符串当作流来读取。
小弟业余爱好自己写点小程序自用,以前用VB6,现在想学学.net
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
With OpenFileDialog2
.Filter = "桩号文件(.txt)|*.txt"
.FilterIndex = 1
.ShowDialog()
End With
Dim ZhhaoFile As String
ZhhaoFile = OpenFileDialog2.FileName
Dim objFile As New System.IO.StringReader(ZhhaoFile)
Dim StrContents As String
StrContents = objFile.ReadToEnd()
objFile.Close()
objFile.Dispose()
TextBox1.Text = StrContents
End Sub
请问这段代码,最后的textbox1里面得到的为什么是路径?譬如"c:\text.txt"之类,而不是文件内容。
要怎样才能读取文本文件内容?
谢谢!
------解决方案--------------------
不应该用StringReader而应该用StreamReader,StringReader会把一个字符串当作流来读取。