读取txtvb.net的简单介绍

vb.net的richtextbook控件要如何读取TXT中的文本 。RichTextBox1.LoadFile("E:\MVP.txt", RichTextBoxStreamType.PlainText),关键是 PlainText 参数,这样文本框就能显示你指定路径的文本文件
vb.net 怎样从txt取得内容?读操作
Dim line As String
Dim sr As StreamReader = New StreamReader(strPath,System.Text.Encoding.Default)
Do While sr.Peek()0
line = sr.ReadLine()
Loop
sr.Close()
sr = Nothing
注释:
构造函数new
Public Sub New(Path,Encoding)
path:要打开文件的完整路径,如果文件抛出一个错误 。
Encoding:缺省值为System.Text.Encoding.Default , 即使用系统缺省的编码,指示以什么样的编码读文件 。
----------------------------------------------------------------
读取c:\test.txt文件的某行
Set fso = CreateObject("Scripting.FileSystemObject")
Dim LineNumber As Integer
Set ts = fso.OpenTextFile("c:\test.txt", 1)
LineNumber = InputBox("请输入行号")
While Not ts.AtEndOfStream
If ts.Line = LineNumber Then Text1.Text = ts.ReadLine
ts.ReadLine
Wend
ts.Close
VB.NET 怎么获取txt特定字段后的内容? Dim str As String = My.Computer.FileSystem.ReadAllText("D:\Demo.txt")
Dim str_search As String = "profile=" '定义要搜索的字符串
TextBox1.Text = str.Substring(InStr(str, str_search) + str_search.Length - 1)
vb.net读写txt文件的问题这个用读写TXT的话比较繁琐,建议题主使用读写INI的方式,参考:
'声明INI配置文件读写API函数
【读取txtvb.net的简单介绍】Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32
'定义读取配置文件函数
Public Function GetINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As String
Dim Str As String = LSet(Str, 256)
GetPrivateProfileString(Section, AppName, lpDefault, Str, Len(Str), FileName)
Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1)
End Function
'定义写入配置文件函数
Public Function WriteINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As Long
WriteINI = WritePrivateProfileString(Section, AppName, lpDefault, FileName)
End Function
'定义INI文件位置
Dim path As String
path = "C:\t861239263.ini" '假设是这个文件
那么读取的时候:
TextBox1.Text = GetINI("main","v1","", path)
TextBox2.Text = GetINI("main","v2","", path)
TextBox3.Text = GetINI("main","v3","", path)
写入的时候:
WriteINI("main","v1", TextBox1.Text, path)
WriteINI("main","v2", TextBox2.Text, path)
WriteINI("main","v3", TextBox3.Text, path)
【附】INI文件的格式,范例:
[main]
v1=133233
v2=132387
v3=322333
[aaa]
abc=1
bcd=2
[bbb]
ni=wo
wo=ta
请教在VB.net中如何将数据写入txt文件、再从txt文件读出?软糖来告诉你吧 。
VB.net中读写文件主要使用System.IO命名空间 。
① 使用 File.ReadAllText 读取
Dim s As String = System.IO.File.ReadAllText("C:\a.txt")
② 使用 StreamReader 读取读取txtvb.net,注意编码格式和写入读取txtvb.net的编码保持一致 。
Dim sr As StreamReader = New StreamReader("C:\a.txt", System.Text.Encoding.UTF8)

推荐阅读