vb.net如何在读取txt文本时,保留原有格式?(空格、换行符)

我用vb.net读取文本文件,并且赋值给一个string变量。本来有好几行的,读出来只有1行,该怎么办?
我是用
FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
Dim str As String = LineInput(1)

lineinput只能读取第一行,而我想保留格式读取整个文件,不知道该怎么办

'教你一个高招(VB.NET 4)
'因为Environment.NewLine.ToString是换行符,所以:

'这是TextBox控件
TextBox1.Text &= "字符串" & Environment.NewLine.ToString

'这是ListBox1控件
ListBox1.Itmes.add("字符串" & Environment.NewLine.ToString)
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-09-08
dim Tmp as string
line input #1,tmp
str=str & vbcrlf & tmp

自己补上换行符追问

我想自动读取,选一个文件,读出来就有换行符

追答

VB.net 没用过不知道和VB6的具体差异!
VB6下读一个文件:
OpenFileDialog1.showopen '这句打开对话框选取文件
open OpenFileDialog1.FileName for input as #1
dim Str as string
do while not eof(1)
dim tmp as string
line input #1,tmp
str=str & vbcrlf & tmp
'上面简化了开头会多一空行,/完整的:STR=STR & IIF(STR="","",vbcrlf ) & tmp
loop
close #1

本回答被提问者采纳
第2个回答  2011-09-08
dim Tmp as string
line input #1,tmp
str=str & vbcrlf & tmp

自己补上换行符追问

我想自动读取,选一个文件,读出来就有换行符

追答

VB.net 没用过不知道和VB6的具体差异!
VB6下读一个文件:
OpenFileDialog1.showopen '这句打开对话框选取文件
open OpenFileDialog1.FileName for input as #1
dim Str as string
do while not eof(1)
dim tmp as string
line input #1,tmp
str=str & vbcrlf & tmp
'上面简化了开头会多一空行,/完整的:STR=STR & IIF(STR="","",vbcrlf ) & tmp
loop
close #1

本回答被提问者采纳
相似回答
大家正在搜