close

讀xxx=xxx  此種ini檔

讀取INI文件設置信息

雖然VB.NET中讀取XML配置信息很方便,但有時開發的過程中還是要用到INI文件,在VB.NET中讀取INI卻不像VB中那麼方便了,剛才寫了個函數,現貼出來,也許各位能用得上。


    』函數名: sdGetIniInfo
    』功能:讀取INI文件設置信息
    』參數說明:iniFile-->INI文件     iniSection--INI文件中設置的部分名稱
    』作者:SD
    』日期:2005-10-11
    』Email:ztqas@126.com
    』備註:轉載或修改請保留此信息
    Function sdGetIniInfo(ByVal iniFile As String, ByVal iniSection As String) As String
        If Not File.Exists(iniFile) Then
            Return "文件 " & iniFile & " 未找到,請確認路徑和文件名是否正確!"
            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 & " 的設置信息!"
        End If
    End Function

說明:在引用的面頁中要先引用 Imports System.IO

EG:

set.ini文件內容:

[Info]
name=zhuang
age=20

    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


作者Blog:http://blog.csdn.net/TSD/

 

馬上加入有廣告下線的廣告!!

幫你點廣告!!

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 11 的頭像
    11

    冠霖的部落格

    11 發表在 痞客邦 留言(0) 人氣()