VB.NET讀寫INI配置文件
成都創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)網(wǎng)站建設(shè)與網(wǎng)站營銷,提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站開發(fā)、seo優(yōu)化、網(wǎng)站排名、互聯(lián)網(wǎng)營銷、微信小程序定制開發(fā)、公眾號(hào)商城、等建站開發(fā),成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)策劃專家,為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制解決方案,幫助客戶在新的全球化互聯(lián)網(wǎng)環(huán)境中保持優(yōu)勢(shì)。
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
用API吧
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
'讀
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 WriteString As String, ByVal FileName As String) As Long
WriteINI = WritePrivateProfileString(Section, AppName, WriteString, FileName)
End Function
其中,Section是像[ip]那樣的(填里面的就可以了),AppName是“=”前面的項(xiàng)名稱,lpDefault是如果沒有可用的值,就返回lpDefault,F(xiàn)ileName是文件名,WriteString是要寫入的值
你所希望的:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(GetINI("ip", "user_id", "", "D:\DataBase.ini"))
'返回sa
End Sub
1,對(duì)于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è)可用的文件號(hào)
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
名字有規(guī)律,就可以for循環(huán)。
名字沒有規(guī)律,可以想辦法讓他有規(guī)律,一種方法是設(shè)置包含所有節(jié)名稱的string數(shù)組,再循環(huán)。