iniファイルの読み込み


iniファイルを読み込むサンプル

Public Class Form1
    '
    '   APIの定義
    '
    Declare Function GetPrivateProfileString _
        Lib "KERNEL32.DLL" Alias "GetPrivateProfileStringA" ( _
            ByVal lpAppName As String, _
            ByVal lpKeyName As String, _
            ByVal lpDefault As String, _
            ByVal lpReturnedString As String, _
            ByVal nSize As Integer, _
            ByVal lpFileName As String) As Integer

    '
    '   テスト用ドライバ
    '
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strVar As String

        'iniファイルの取得
        strVar = GetIniValue("HOGE", "VAL1", "", "D:\test.ini")
        MsgBox(strVar)

        strVar = GetIniValue("HOGE", "VAL2", "", "D:\test.ini")
        MsgBox(strVar)

    End Sub


    '   Iniファイル読み込み
    '
    '   inSection   : セクション名
    '   inKeyName   : 項目名
    '   inDefaults  : 項目が存在しない場合の初期値
    '   inFilename  : ファイル名
    Public Function GetIniValue(ByVal inSection As String, _
                           ByVal inKeyName As String, _
                           ByVal inDefaults As String, _
                           ByVal inFilename As String _
                          ) As String

        Dim strResult As String = Space(255)

        Call GetPrivateProfileString(inSection, inKeyName, inDefaults, _
                                 strResult, Len(strResult), _
                                 inFilename)

        GetIniValue = Microsoft.VisualBasic.Left(strResult, InStr(strResult, Chr(0)) - 1)
    End Function



End Class

動作確認環境:Visual Stadio 2005