1,對于INI文件,可以當(dāng)做像TXT文件一樣來進行讀取和寫入。
創(chuàng)新互聯(lián)公司是一家從事企業(yè)網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、做網(wǎng)站、行業(yè)門戶網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計制作的專業(yè)網(wǎng)站設(shè)計公司,擁有經(jīng)驗豐富的網(wǎng)站建設(shè)工程師和網(wǎng)頁設(shè)計人員,具備各種規(guī)模與類型網(wǎng)站建設(shè)的實力,在網(wǎng)站建設(shè)領(lǐng)域樹立了自己獨特的設(shè)計風(fēng)格。自公司成立以來曾獨立設(shè)計制作的站點超過千家。
2,先把整個文件度出來,然后找到相應(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?=?""
'獲取一個可用的文件號
Dim?MyFileNum?As?Integer?=?FreeFile()
'打開指定的文件,進行讀取操作
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
VB.NET讀寫INI配置文件
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("錯誤?。。。?)
End Try
End Sub
名字有規(guī)律,就可以for循環(huán)。
名字沒有規(guī)律,可以想辦法讓他有規(guī)律,一種方法是設(shè)置包含所有節(jié)名稱的string數(shù)組,再循環(huán)。