[VB.NET]讀取ini檔案

  • 21211
  • 0

[VB.NET]讀取ini檔案

 

使用ini檔案前, 先簡略介紹ini檔內容
 
; 分號之後內容為註解
 
[section] ;中括號內為節
name=value ;參數名稱=值
 
 
讀取ini檔案在此提供兩種方式(範例皆為Windows Form)之程式碼片段
 
    1. 開啟檔案總管選取ini檔
 
ini檔案
 
; ini file

[file]
type=test

[field]
count=10

 

VB code

   
 'API
    Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
        (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, _
         ByVal lpReturnedString As String, ByRef nSize As Integer, ByVal lpFileName As String) As Integer

    '取得ini檔案內容
    Public Function GetIniInfo(ByVal pSection As String, ByVal pKey As String, ByVal AppPath As String)
        Return GetIniString(pSection, pKey, AppPath)
        'Return Nothing
    End Function

    Public Function GetIniString(ByVal lkSection As String, ByVal lkKey As String, ByVal lkIniPath As String) As String
        Dim lngRtn As Long
        Dim strRtn As String

        GetIniString = ""
        strRtn = New String(Chr(20), 255)

        Dim fi As New System.IO.FileInfo(lkIniPath)
        If Not fi.Exists Then
            Throw New Exception("設定檔不存在(" & lkIniPath & ")")
        End If

        lngRtn = GetPrivateProfileString(lkSection, lkKey, vbNullString, strRtn, Len(strRtn), lkIniPath)
        If lngRtn <> 0 Then
            'GetIniString = Left$(strRtn, InStr(strRtn, Chr(0)) - 1)
            GetIniString = strRtn.Substring(0, InStr(strRtn, Chr(0)) - 1)
        End If

    End Function    


    Private Sub btImportIniFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btImportIniFile.Click
        Dim myStream As System.IO.Stream
        OpenFileDialog1.InitialDirectory = "c:\" '預設開啟c槽
        OpenFileDialog1.Filter = "ini files (*.ini)|*.ini" '限制只能選擇ini檔
        OpenFileDialog1.FilterIndex = 2
        OpenFileDialog1.RestoreDirectory = True

        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            Me.lbImportIniFileValue.Text = OpenFileDialog1.FileName '顯示選取的檔案完整路徑及名稱
            myStream = OpenFileDialog1.OpenFile()
            If Not (myStream Is Nothing) Then
                myStream.Close()
            End If
        End If
    End Sub

    Private Sub btSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSubmit.Click

        Dim iniContent As New GetIniContent()
        Dim iniFileName As String = Me.lbImportIniFileValue.Text
        Dim fileType As String = ""
        Dim fieldCount As Integer = 0

        Try
            '判斷設定檔是否存在
            If File.Exists(iniFileName) Then
                '取得ini檔的各參數值
                '[file]裡的type
                fileType = GetIniInfo("file", "type", iniFileName)
                '[field]裡的count
                fieldCount = CType(GetIniInfo("field", "count", iniFileName), Int32)
            Else
                MsgBox("發生錯誤: " & iniFileName & " 檔案不存在!")
            End If
        Catch ex As Exception
            MsgBox("發生錯誤: " & ex.Message)
        End Try
    End Sub
 
    2. 指定目錄讀取ini檔
 
ini檔案
 
; ini file

[file]
type=test

[field]
count=10

 

VB code

 'API
    Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
        (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, _
         ByVal lpReturnedString As String, ByRef nSize As Integer, ByVal lpFileName As String) As Integer

    '取得ini檔案內容
    Public Function GetIniInfo(ByVal pSection As String, ByVal pKey As String, ByVal AppPath As String)
        Return GetIniString(pSection, pKey, AppPath)
        'Return Nothing
    End Function

    Public Function GetIniString(ByVal lkSection As String, ByVal lkKey As String, ByVal lkIniPath As String) As String
        Dim lngRtn As Long
        Dim strRtn As String

        GetIniString = ""
        strRtn = New String(Chr(20), 255)

        Dim fi As New System.IO.FileInfo(lkIniPath)
        If Not fi.Exists Then
            Throw New Exception("設定檔不存在(" & lkIniPath & ")")
        End If

        lngRtn = GetPrivateProfileString(lkSection, lkKey, vbNullString, strRtn, Len(strRtn), lkIniPath)
        If lngRtn <> 0 Then
            'GetIniString = Left$(strRtn, InStr(strRtn, Chr(0)) - 1)
            GetIniString = strRtn.Substring(0, InStr(strRtn, Chr(0)) - 1)
        End If

    End Function

    '讀取ini
    Private Sub LoadIni(ByVal currentDir As String)
        Dim iniDir As String = "ini"
        Dim myDir As DirectoryInfo = New DirectoryInfo(currentDir + iniDir)
        Dim fileType As String = ""
        Dim fieldCount As Integer = 0
        Try
            If FileIO.FileSystem.DirectoryExists(currentDir + "/" + iniDir) Then
                For Each myFile As FileInfo In myDir.GetFiles
                    If myFile.Exists Then
                        '取得ini檔的各參數值
                        '[file]裡的type
                        fileType = GetIniInfo("file", "type", myFile.FullName)
                        '[field]裡的count
                        fieldCount = CType(GetIniInfo("field", "count", myFile.FullName), Int32)
                    Else
                        MsgBox("發生錯誤: " & myFile.Name & "檔案不存在!")
                    End If
                Next
            Else
                MsgBox("發生錯誤: " & myDir.Name  & "目錄不存在!")
            End If
        Catch ex As Exception
            MsgBox("LoadIni error:" + ex.Message + Chr(10) + ex.ToString)
        End Try
    End Sub

 

https://www.facebook.com/wenming.yan