最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

VB READ INT AND WRITE INI

2023-06-03 15:10 作者:MMCjelly  | 我要投稿

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

VB READ INT AND WRITE INI的評論 (共 條)

分享到微博請遵守國家法律
汉中市| 宜君县| 铜川市| 定远县| 进贤县| 金门县| 东至县| 饶河县| 东光县| 西贡区| 错那县| 新化县| 灵川县| 齐齐哈尔市| 东方市| 古田县| 黎城县| 都江堰市| 绵阳市| 亳州市| 曲阳县| 独山县| 泰宁县| 玛纳斯县| 民县| 伊吾县| 兴国县| 布尔津县| 于都县| 治县。| 镇宁| 临沭县| 灵武市| 星子县| 宜川县| 泽库县| 忻州市| 怀宁县| 陆河县| 加查县| 焉耆|