❶ vb.net中,读取和写入文件
写入:Dim sr As New IO.StreamWriter(Application.StartupPath & "/写入的文本.txt")
sr.WriteLine("写入的内容") sr.Close()读取:If (File.Exists(Application.StartupPath & "/msg.txt")) Then
Dim fm As New IO.FileStream(Application.StartupPath & "/读取的文本.txt", FileMode.Open)
Dim sr As IO.StreamReader = New IO.StreamReader(fm)
Do While sr.Peek() >= 0
TextBox1.Text = sr.ReadLine() (读取文本到文本框)
Loop end if
❷ vb.net 磁盘文件列表(*.txt),并能读取txt文件。
读取每行TXT如下代码:
VB.NET code Public Sub ReadFileSample() '打开程序当前路径下的config.txt文件 '内容就是楼主贴出来的 Dim reader As TextReader = File.OpenText("config.txt") Dim line As String = reader.ReadLine() '读第一行 line = reader.ReadLine() '读第二行 line = reader.ReadLine() '读第三行 Dim n As Integer = 3 '当前行号 While line <> "" And n < 50 line = reader.ReadLine() '读下一行 n = n + 1 End While Dim items As String() = line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) For Each item As String In items Console.WriteLine(item) Next End Sub
❸ 怎样用vb.net读取本地.txt文件
imports System.IO
读取指定文件
'
'读取指定文本文件
Public Function readtext(ByVal path As String)
If path = "" Then
readtext = "操作失败!"
Exit Function
End If
Try
If File.Exists(path) = True Then
Dim fs As New FileStream(path, FileMode.Open)
Dim sr As New StreamReader(fs)
Dim str As String
str = sr.ReadToEnd.ToString
sr.Close()
fs.Close()
readtext = str
Else
readtext = "操作失败!"
End If
Catch ex As Exception
readtext = "操作失败!"
End Try
End Function
'向指定文件写入数据
Public Function writetext(ByVal path As String, ByVal opi As Integer, ByVal msg As String)
If path = "" Then
writetext = "操作失败!"
Exit Function
End If
Dim op As FileMode
Select Case opi
Case 1
op = FileMode.Append
Case 2
op = FileMode.Create
Case Else
op = FileMode.Create
End Select
Try
If File.Exists(path) = True Then
Dim fs As New FileStream(path, op)
Dim sr As New StreamWriter(fs)
sr.WriteLine(msg)
sr.Close()
fs.Close()
writetext = "操作完成!"
Else
writetext = "操作失败!"
End If
Catch ex As Exception
writetext = "操作失败!"
End Try
End Function
参考这个吧
'
'vb.net源代码来自www.c-pet.com
'
❹ VB.net 如果读取txt数据(或十进制dat数据)
Imports System.IO
Public Class Form1
'按钮1执行写文件操作
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim w As New StreamWriter(文件标识符, System.Text.Encoding.Default) ‘用StreamWriter打开文件写
w.Write(TextBox1.Text) ‘将文本框TextBox1.Text里的内容执行写操作,写入打开的文件
w.Close() ‘关闭对象
End Sub
'按钮2执行读文件操作
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim r As New StreamReader(文件标识符, System.Text.Encoding.Default) ‘用StreamReader打开文件读
Dim s As String
TextBox1.Text = ""
Do While r.Peek > -1 '是否到文件尾
s = r.ReadLine ‘从打开的文件中读取一行内容
TextBox1.Text = TextBox1.Text & s & vbCrLf ‘添加到文本框TextBox1.Text的后面并换行
Loop
r.Close() ‘关闭对象
End Sub
End Class
❺ VB.net窗体设计中,如何读取.txt文件中的数据
1、新建一个标准的VB EXE工程,只有一个Form,Form上有两个按钮:Command1和Command2。
2、双击Command1添加如下代码
Private Sub Command1_Click()
Dim strFile As String
Dim intFile As Integer
Dim strData As String
strFile = "c:\学生成绩.txt"
intFile = FreeFile
Open strFile For Input As intFile
strData = StrConv(InputB(FileLen(strFile), intFile), vbUnicode)
Debug.Print strData
Close intFile
End Sub
3、按F8开始单步调试代码,点击Command1,进入单步调试功能,
4、多次按下F8或直接按下F5运行完成,就完成了读取文本文件内容并输出到立即窗口。
❻ 如何用vb.net编写读取txt内容的代码
窗体上添加2个文本框,设置成多行,2个按钮,在文本框1里随便输入若干文字,可以多行,单击按钮1,保存到文件。然后单击按钮2,把刚才写入的文件读到文本框2里。
代码如下:
'写文本文件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'创建(写入)一个文本文件
Dim MyStream As New System.IO.FileStream(Application.StartupPath & "\Ssk.txt", System.IO.FileMode.Create)
Dim MyWriter As New System.IO.StreamWriter(MyStream, System.Text.Encoding.Default)
MyWriter.WriteLine(TextBox1.Text)
MyWriter.Flush()
MyWriter.Close()
MyStream.Close()
End Sub
'读文本文件
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'读取一个文本文件
Dim MyReader As New System.IO.StreamReader(Application.StartupPath & "\Ssk.txt", System.Text.Encoding.UTF8)
TextBox2.Text = MyReader.ReadToEnd()
MyReader.Close()
End Sub
气斜射入水或其他介质,折射光线与入射光线法线在
❼ vb.net如何打开选定文件夹下所有TXT文件,读取数据,写入数据,并保存数据至新的文件
IfFolderBrowserDialog.ShowDialog=Windows.Forms.DialogResult.OKThen
Dim资料夹AsString()=System.IO.Directory.GetFiles(FolderBrowserDialog.SelectedPath,"*")
ForEach文件In资料夹
MsgBox(My.Computer.FileSystem.ReadAllText(文件))'读取数据
My.Computer.FileSystem.WriteAllText(文件,"数据",False)'写入数据
Next
EndIf
EndUsing