❶ 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