vb.net數(shù)據(jù)儲(chǔ)存 vbnet using

vb.net 如何存儲(chǔ)用戶數(shù)據(jù)?

這個(gè)你可以用 api操作ini文件來實(shí)現(xiàn)。ini是windows系統(tǒng)的配置文件。

成都創(chuàng)新互聯(lián)公司于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目做網(wǎng)站、成都做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元依蘭做網(wǎng)站,已為上家服務(wù),為依蘭各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792

'首先聲明這兩個(gè)api

Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long

Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

vb.net儲(chǔ)存文本數(shù)據(jù)方法

Private Sub Command1_Click()

'寫文件示例

Dim strFileName As String '文件名

Dim lngHandle As Long '句柄

Dim strWrite As String '要寫入的文本內(nèi)容

strFileName = App.Path "\a.txt"

''''''''App.Path "\a.txt"相對(duì)路徑

''"c:\w.txt"絕對(duì)路徑

lngHandle = FreeFile() '取得句柄

'準(zhǔn)備要寫入的內(nèi)容

strWrite = Text1.Text '或者 strWrite = "這些文字將被寫入文件。"

'For后面的參數(shù)表示以何種方式打開文件,Input是讀取,Output是覆蓋寫入,Append是追加寫入

''''''append是每次在文件末尾寫入,不刪除其它已經(jīng)存在的文件.如果換成output則刪除其它文件后再寫入

''''''print#1,text1.text ''''''如果print換成write則寫進(jìn)txt中后自動(dòng)加雙引號(hào)

Open strFileName For Output As lngHandle '打開文件

Print #lngHandle, strWrite '寫入文本

Close lngHandle '關(guān)閉文件

End Sub

Private Sub Command2_Click()

'讀文件示例

Dim strFileName As String '文件名

Dim lngHandle As Long '文件句柄

Dim strAll As String '所讀取的文本文件的所有內(nèi)容

Dim strLine As String '在循環(huán)中存放每行的內(nèi)容

strFileName = App.Path "\a.txt"

'獲得文件的句柄

lngHandle = FreeFile()

'For后面的參數(shù)表示以何種方式打開文件,Input是讀取,Output是覆蓋寫入,Append是追加寫入

Open strFileName For Input As lngHandle

'循環(huán)直到文件尾

Do While Not EOF(lngHandle)

'每次讀取一行存放在strLine變量中

Line Input #lngHandle, strLine

'每次讀取都把所讀到的內(nèi)容連接到strAll變量,由于Line Input去掉了換行符,所以這里補(bǔ)上

strAllstrAll = strAll strLine vbCrLf

Loop

'顯示得到的全部分內(nèi)容

MsgBox strAll

End Sub

Private Sub Command1_Click()

Open App.Path "\a.txt" For Output As #1

Print #1, Text1.Text '這里可以是數(shù)據(jù)本身也可以是目標(biāo)控件的屬性

Close #1 '關(guān)閉打開的文件

End Sub

'For后面的參數(shù)表示以何種方式打開文件,Input是讀取,Output是覆蓋寫入,Append是追加寫入

append是每次在文件末尾寫入,不刪除其它已經(jīng)存在的文件.如果換成output則刪除其它文件后再寫入 print#1,text1.text 如果print換成write則寫進(jìn)txt中后自動(dòng)加雙引號(hào),以上就是數(shù)據(jù)保存為VB.NET TXT文件的代碼。

VB如何將程序的數(shù)據(jù)保存?

VB6.0

寫入:

Open "D:\123.txt" For Output As #1 '打開XXX路徑的XXX文件(雙引號(hào)里表示文件位置和文件名)

Print #1, Text1.Text '寫入Text1的Text內(nèi)容

Close #1 '關(guān)閉

讀取:

Open "D:\123.txt" For Input As #1 '打開打開XXX路徑的XXX文件(雙引號(hào)里表示文件位置和文件名)

Do While Not EOF(1)

Line Input #1, s

Text1.Text = s

Loop 'Do...Loop表示循環(huán)讀取文件的內(nèi)容,并讓Text1.Text=內(nèi)容

Close #1 '關(guān)閉

VB.net中讀寫文件主要使用System.IO命名空間。

① 使用?File.ReadAllText 讀取

Dim s As String = System.IO.File.ReadAllText("C:\a.txt")

② 使用 StreamReader 讀取,注意編碼格式和寫入的編碼保持一致。

Dim sr As StreamReader = New StreamReader("C:\a.txt", System.Text.Encoding.UTF8)

Dim s As String = sr.ReadToEnd()

sr.Close()

網(wǎng)頁(yè)標(biāo)題:vb.net數(shù)據(jù)儲(chǔ)存 vbnet using
分享網(wǎng)址:http://muchs.cn/article18/dossodp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站維護(hù)、定制網(wǎng)站、微信小程序、網(wǎng)站營(yíng)銷、App設(shè)計(jì)

廣告

聲明:本網(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í)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司