1、新建一個(gè)標(biāo)準(zhǔn)的VB EXE工程,只有一個(gè)Form,F(xiàn)orm上有兩個(gè)按鈕:Command1和Command2。

創(chuàng)新互聯(lián)專(zhuān)注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、大埔網(wǎng)絡(luò)推廣、微信小程序、大埔網(wǎng)絡(luò)營(yíng)銷(xiāo)、大埔企業(yè)策劃、大埔品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供大埔建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:chinadenli.net
2、雙擊Command1添加如下代碼
Private Sub Command1_Click()
Dim strFile? ? ?As String
Dim intFile? ? ?As Integer
Dim strData? ? ?As String
strFile = "c:\學(xué)生成績(jī).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開(kāi)始單步調(diào)試代碼,點(diǎn)擊Command1,進(jìn)入單步調(diào)試功能,
4、多次按下F8或直接按下F5運(yùn)行完成,就完成了讀取文本文件內(nèi)容并輸出到立即窗口。
比如說(shuō)用Richtext控件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim file As String = "f:/x.txt"
RichTextBox1.LoadFile(file)
MsgBox("ok")
End Sub
一個(gè)按鈕,一個(gè)文本框,把文本框設(shè)置成多行顯示。
代碼如下:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim r As New StreamReader(Application.StartupPath "\ABC.txt", System.Text.Encoding.Default) '用StreamReader打開(kāi)文本文件
Dim s As String
TextBox1.Text = ""
Do While r.Peek -1 '是否到文件尾
s = r.ReadLine '從打開(kāi)的文件中讀取一行內(nèi)容
TextBox1.Text = TextBox1.Text s vbCrLf '添加到文本框TextBox1.Text的后面并回車(chē)
Loop
r.Close() '關(guān)閉對(duì)象
End Sub
End Class
軟糖來(lái)告訴你吧。
VB.net中讀寫(xiě)文件主要使用System.IO命名空間。
① 使用 File.ReadAllText 讀取
Dim s As String = System.IO.File.ReadAllText("C:\a.txt")
② 使用 StreamReader 讀取,注意編碼格式和寫(xiě)入的編碼保持一致。
Dim sr As StreamReader = New StreamReader("C:\a.txt", System.Text.Encoding.UTF8)
Dim s As String = sr.ReadToEnd()
sr.Close()
③ 使用 File.WriteAllText 寫(xiě)入,會(huì)覆蓋同名的文件。
Dim 要寫(xiě)的內(nèi)容 As String = ""
File.WriteAllText(文件路徑, 要寫(xiě)的內(nèi)容, System.Text.Encoding.UTF8)
④ 使用 StreamWriter 寫(xiě)入。
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter("C:\a.txt", False, System.Text.Encoding.UTF8)
sw.WriteLine(TextTB.Text)
sw.Close()
⑤ 使用 StreamWriter 追加寫(xiě)入。
將上面代碼的第二個(gè)參數(shù)False改為T(mén)rue。
◆ 滿意請(qǐng)采納,謝謝 ◆
網(wǎng)站題目:vb.net導(dǎo)入txt的簡(jiǎn)單介紹
瀏覽路徑:http://chinadenli.net/article0/hgpeoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、商城網(wǎng)站、網(wǎng)站收錄、自適應(yīng)網(wǎng)站、網(wǎng)站營(yíng)銷(xiāo)、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)