imports System.IO

創(chuàng)新互聯(lián)是一家集策劃、設(shè)計(jì)、技術(shù)開(kāi)發(fā)一體的專業(yè)網(wǎng)站制作公司,技術(shù)團(tuán)隊(duì)10年來(lái)致力于為客戶提供企業(yè)網(wǎng)站定制,手機(jī)網(wǎng)站制作設(shè)計(jì)。經(jīng)過(guò)多年發(fā)展,公司技術(shù)團(tuán)隊(duì),先后服務(wù)了近千家客戶,包括各類中小企業(yè)、上市公司、高校、政府。公司在過(guò)去10年的資源積累,追求并一直堅(jiān)持,為客戶打造更有價(jià)值的互聯(lián)網(wǎng)平臺(tái)。
讀取指定文件
'
'讀取指定文本文件
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
'向指定文件寫(xiě)入數(shù)據(jù)
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
樓上的繼續(xù)忽悠人吧。2,3句搞定的東西弄這么復(fù)雜。。。
就是讀取服務(wù)器文件呀。
微軟論壇就有例子。
Imports System
Imports System.IO
Class Test
Public Shared Sub Main()
Try
' 創(chuàng)建一個(gè)實(shí)例的StreamReader閱讀從一個(gè)文件。
Dim sr As StreamReader = New StreamReader("TestFile.txt")
Dim line As String
' 閱讀并顯示線路從文件,直到最后
' 該文件被達(dá)成。
Do
line = sr.ReadLine()
Console.WriteLine(Line)
Loop Until line Is Nothing
sr.Close()
Catch E As Exception
' 讓用戶知道有什么地方出了差錯(cuò)。
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
End Try
End Sub
End Class
//上面是微軟的例子,你可以參考自己改,下面是我改的。
Imports System.IO
Partial Class test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using sr As StreamReader = New StreamReader("E:\新建文本文檔.txt", Encoding.GetEncoding("gb2312"))
Response.Write(sr.ReadLine())
End Using
End Sub
End Class
已經(jīng)測(cè)試過(guò)了,文件路徑自己改,支持TXT格式,其他格式自己修改編碼
1、新建一個(gè)標(biāo)準(zhǔn)的VB EXE工程,只有一個(gè)Form,F(xiàn)orm上有兩個(gè)按鈕:Command1和Command2。
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)容并輸出到立即窗口。
當(dāng)前名稱:vb.net讀寫(xiě)配置文件 vbnet讀取文本
文章起源:http://chinadenli.net/article14/dojoede.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)站維護(hù)、App開(kāi)發(fā)、微信公眾號(hào)
聲明:本網(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)