VB.NET讀寫INI配置文件
創(chuàng)新互聯(lián)建站是專業(yè)的陵水黎族網(wǎng)站建設(shè)公司,陵水黎族接單;提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行陵水黎族網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim path As String
path = Application.StartupPath + "\Send.ini"
TextBox1.Text = GetINI("Send", "Send1", "", path)
TextBox2.Text = GetINI("Send", "Send2", "", path)
Dim IsSms As Integer = GetINI("Send", "IsSms", "", path)
If (IsSms = 1) Then
Me.RadioButton1.Checked = True
ElseIf (IsSms = 0) Then
Me.RadioButton2.Checked = True
End If
End Sub
Public Function GetINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As String
Dim Str As String = LSet(Str, 256)
GetPrivateProfileString(Section, AppName, lpDefault, Str, Len(Str), FileName)
Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1)
End Function
Public Function WriteINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As Long
WriteINI = WritePrivateProfileString(Section, AppName, lpDefault, FileName)
End Function
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim path As String
path = Application.StartupPath + "\Send.ini"
WriteINI("Send", "Send1", TextBox1.Text, path)
WriteINI("Send", "Send2", TextBox2.Text, path)
If (Me.RadioButton1.Checked = True) Then
WriteINI("Send", "IsSms", 1, path)
ElseIf (Me.RadioButton2.Checked = True) Then
WriteINI("Send", "IsSms", 0, path)
End If
MsgBox("配置設(shè)置已經(jīng)成功!!!!")
Catch ex As Exception
MsgBox("錯(cuò)誤!!!!")
End Try
End Sub
1,對于INI文件,可以當(dāng)做像TXT文件一樣來進(jìn)行讀取和寫入。
2,先把整個(gè)文件度出來,然后找到相應(yīng)行刪除(拋棄)以后,再重新寫入文件。
Public?Class?Form1
Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click
Dim?MyStr?As?String?=?""
Dim?AllStr?As?String?=?""
'獲取一個(gè)可用的文件號
Dim?MyFileNum?As?Integer?=?FreeFile()
'打開指定的文件,進(jìn)行讀取操作
FileOpen(MyFileNum,?"C:\My.ini",?OpenMode.Input)
Do?While?Not?EOF(MyFileNum)
'讀取一行
MyStr?=?LineInput(MyFileNum)
If?MyStr??"b=2"?Then
If?AllStr?=?""?Then
AllStr?=?AllStr??MyStr
Else
AllStr?=?AllStr??vbCrLf??MyStr
End?If
End?If
Loop
FileClose(MyFileNum)???'關(guān)閉文件
'寫文件
Dim?MyStream?As?New?System.IO.FileStream("C:\My.ini",?IO.FileMode.Create)
Dim?MyWriter?As?New?System.IO.StreamWriter(MyStream,?System.Text.Encoding.UTF8)
MyWriter.WriteLine(AllStr)
MyWriter.Flush()
MyWriter.Close()
MyStream.Close()
End?Sub
End?Class
Dim?s?As?String()?=?System.IO.File.ReadAllLines("t.ini")?'文件
For?i?As?Integer?=?0?To?s.Length?-?1
Dim?si?As?Integer?=?s(i).IndexOf("=")
If?si??-1?Then
ListBox1.Items.Add(s(i).Substring(0,?si)??"?=?"??s(i).Substring(si?+?1))
End?If
Next
's(i).Substring(0,?si)是變量名,s(i).Substring(si?+?1)是值
INI文件不便于操作,使用Settings跟簡單、高效!
先添加一個(gè)Settings設(shè)置
再調(diào)用這個(gè)參數(shù)
就算重新啟動(dòng) 這個(gè)參數(shù)還是可以保留
沒聽懂?
兩句說不清楚,可以遠(yuǎn)程協(xié)助。
網(wǎng)站名稱:vb.net.ini的簡單介紹
分享URL:http://chinadenli.net/article12/dsiscdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、網(wǎng)站排名、定制網(wǎng)站、電子商務(wù)、網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)