簡(jiǎn)單講述VB.NET讀取INI
作者:佚名
這里介紹VB.NET讀取INI,雖然VB.NET中讀取XML配置信息很方便,但有時(shí)開發(fā)的過程中還是要用到INI文件,VB.NET讀取INI卻不像VB中那么方便了,剛才寫了個(gè)函數(shù),現(xiàn)貼出來(lái),也許各位能用得上。
VB.NET還是比較常用的,于是我研究了一下VB.NET讀取INI,在這里拿出來(lái)和大家分享一下,希望對(duì)大家有用。
雖然VB.NET中讀取XML配置信息很方便,但有時(shí)開發(fā)的過程中還是要用到INI文件,VB.NET讀取INI卻不像VB中那么方便了,剛才寫了個(gè)函數(shù),現(xiàn)貼出來(lái),也許各位能用得上。
- Function sdGetIniInfo(ByVal iniFile As String,
ByVal iniSection As String) As String- If Not File.Exists(iniFile) Then
- Return "文件 " & iniFile & " 未找到,請(qǐng)確認(rèn)路徑和文件名是否正確!"
- Exit Function
- End If
- Dim iniRead As New StreamReader(iniFile)
- Dim iniStr As String = iniRead.ReadToEnd
- Dim i As Integer
- Dim cLine As Integer
- Dim noSec As Boolean = False
- Dim getValue As String = ""
- Dim cLst
- cLst = iniStr.Split(Chr(13))
- cLine = UBound(cLst)
- For i = 0 To cLine
- If cLst(i).indexof("=") > 0 Then
- If cLst(i).split("=")(0).trim() = iniSection Then
- noSec = True
- getValue = cLst(i).split("=")(1).trim()
- Exit For
- End If
- End If
- Next
- If noSec = True Then
- Return getValue
- Else
- Return "沒有找到 " & iniSection & " 的設(shè)置信息!"
- End If
- End Function
說(shuō)明:在引用的面頁(yè)中要先引用 Imports System.IO
set.ini文件內(nèi)容:
- Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click- Dim name As String
- name = sdGetIniInfo(Application.StartupPath & "\set.ini", "name")
- MsgBox(name)
- End Sub
以上介紹VB.NET讀取INI。
【編輯推薦】
責(zé)任編輯:佚名
來(lái)源:
IT專家網(wǎng)