VB READ INT AND WRITE INI
Imports System.IO Module iniFile ??' 定義一個函數(shù),用來讀取ini文件中的指定鍵值 ??Function ReadIni(ByVal fileName As String, ByVal section As String, ByVal key As String) As String ????' 創(chuàng)建一個StreamReader對象,打開ini文件 ????Using sr As StreamReader = New StreamReader(fileName) ??????Dim line As String ' 用來存儲每一行的文本 ??????Dim foundSection As Boolean = False ' 用來標(biāo)記是否找到了目標(biāo)節(jié) ??????' 逐行讀取文件,直到找到目標(biāo)鍵或者文件結(jié)束 ??????line = sr.ReadLine() ??????While (line <> Nothing) ????????' 去掉行首和行尾的空白字符 ????????line = line.Trim() ????????' 如果是節(jié)名稱,判斷是否是目標(biāo)節(jié) ????????If line.StartsWith("[") And line.EndsWith("]") Then ??????????If line.Equals("[" & section & "]") Then ????????????foundSection = True ' 找到了目標(biāo)節(jié) ??????????Else ????????????foundSection = False ' 找到了其他節(jié),跳過 ??????????End If ????????ElseIf foundSection Then ' 如果已經(jīng)找到了目標(biāo)節(jié),繼續(xù)查找目標(biāo)鍵 ??????????' 用等號分割鍵和值 ??????????Dim parts() As String = line.Split("=") ??????????If parts.Length = 2 Then ' 如果有且只有一個等號,說明是有效的鍵值對 ????????????Dim k As String = parts(0).Trim() ' 鍵 ????????????Dim v As String = parts(1).Trim() ' 值 ????????????If k.Equals(key) Then ' 如果鍵匹配,返回值并結(jié)束函數(shù) ??????????????Return v ????????????End If ??????????End If ????????End If ????????' 讀取下一行 ????????line = sr.ReadLine() ??????End While ????End Using ????' 如果沒有找到目標(biāo)鍵,返回空字符串 ????Return "" ??End Function ??' 定義一個函數(shù),用來寫入ini文件中的指定鍵值 ??Sub WriteIni(ByVal fileName As String, ByVal section As String, ByVal key As String, ByVal value As String) ????' 創(chuàng)建一個臨時文件名,用來存儲修改后的內(nèi)容 ????Dim tempFile As String = Path.GetTempFileName() ????' 創(chuàng)建一個StreamWriter對象,打開臨時文件 ????Using sw As StreamWriter = New StreamWriter(tempFile) ??????' 創(chuàng)建一個StreamReader對象,打開ini文件 ??????Using sr As StreamReader = New StreamReader(fileName) ????????Dim line As String ' 用來存儲每一行的文本 ????????Dim foundSection As Boolean = False ' 用來標(biāo)記是否找到了目標(biāo)節(jié) ????????Dim writtenKey As Boolean = False ' 用來標(biāo)記是否寫入了目標(biāo)鍵值對 ????????' 逐行讀取文件,直到文件結(jié)束或者寫入了目標(biāo)鍵值對 ????????line = sr.ReadLine() ????????While (line <> Nothing) ??????????' 去掉行首和行尾的空白字符 ??????????line = line.Trim() ??????????' 如果是節(jié)名稱,判斷是否是目標(biāo)節(jié)或者其他節(jié) ??????????If line.StartsWith("[") And line.EndsWith("]") Then ????????????If Not writtenKey And foundSection Then? ??????????????' 如果還沒有寫入目標(biāo)鍵值對,并且已經(jīng)找到了目標(biāo)節(jié), ??????????????' 那么在當(dāng)前節(jié)的末尾寫入目標(biāo)鍵值對,并標(biāo)記為已寫入。 ??????????????sw.WriteLine(key & "=" & value) ??????????????writtenKey = True? ????????????End If ????????????If line.Equals("[" & section & "]") Then? ??????????????foundSection = True ' 找到了目標(biāo)節(jié)? ????????????Else? ??????????????foundSection = False ' 找到了其他節(jié)? ????????????End If ????????????sw.WriteLine(line) ' 將當(dāng)前行寫入臨時文件 ??????????ElseIf foundSection Then ' 如果已經(jīng)找到了目標(biāo)節(jié),繼續(xù)查找目標(biāo)鍵 ????????????' 用等號分割鍵和值 ????????????Dim parts() As String = line.Split("=") ????????????If parts.Length = 2 Then ' 如果有且只有一個等號,說明是有效的鍵值對 ??????????????Dim k As String = parts(0).Trim() ' 鍵 ??????????????Dim v As String = parts(1).Trim() ' 值 ??????????????If k.Equals(key) Then ' 如果鍵匹配,修改值并標(biāo)記為已寫入 ????????????????sw.WriteLine(key & "=" & value) ????????????????writtenKey = True ??????????????Else ' 如果鍵不匹配,保持原樣 ????????????????sw.WriteLine(line) ??????????????End If ????????????Else ' 如果不是有效的鍵值對,保持原樣 ??????????????sw.WriteLine(line) ????????????End If ??????????Else ' 如果還沒有找到目標(biāo)節(jié),保持原樣 ????????????sw.WriteLine(line) ??????????End If ??????????' 讀取下一行 ??????????line = sr.ReadLine() ????????End While ????????If Not writtenKey Then? ??????????' 如果還沒有寫入目標(biāo)鍵值對,說明目標(biāo)節(jié)不存在或者目標(biāo)鍵不存在, ??????????' 那么在文件的末尾創(chuàng)建目標(biāo)節(jié)并寫入目標(biāo)鍵值對。 ??????????sw.WriteLine() ??????????sw.WriteLine("[" & section & "]") ??????????sw.WriteLine(key & "=" & value) ????????End If ??????End Using ????End Using ????' 刪除原始文件,并將臨時文件重命名為原始文件名 ????File.Delete(fileName) ????File.Move(tempFile, fileName) ??End Sub End Module