GetPrivateProfileInt Alternative (Kernel32)



Usage

Public Module GetPrivateProfileIntA
    'Public Declare Function GetPrivateProfileIntA Lib "Kernel32.dll" (ApplicationName As String,
    '                                                                  KeyName As String,
    '                                                                  nDefault As Integer,
    '                                                                  FileName As String) As Integer
    <DllImport("kernel32.dll", SetLastError:=True, EntryPoint:="GetPrivateProfileIntA")>
    Public Function GetPrivateProfileIntA(lpApplicationName As String,
                                          lpKeyName As String,
                                          nDefault As Integer,
                                          lpFileName As String) As Integer
    End Function
    Public Sub Test_GetPrivateProfileIntA()
        Dim test As Integer = GetPrivateProfileIntA("Application", "Key1", -1, Application.StartupPath + "\123.ini")
        Dim test2 As Integer = GetPrivateProfileIntA_NoAPI("Application", "Key2", -1, Application.StartupPath + "\123.ini")
    End Sub
    Public Function GetPrivateProfileIntA_NoAPI(ApplicationName As String,
                                                KeyName As String,
                                                nDefault As Integer,
                                                FileName As String) As Integer
        If File.Exists(FileName) Then
            Dim FileName_Strings() As String = File.ReadAllLines(FileName)
            Dim Application_Location As Integer = Array.IndexOf(FileName_Strings, "[" + ApplicationName + "]")
            If Application_Location > -1 Then
                For i = Application_Location To FileName_Strings.Length - 1
                    If FileName_Strings(i).Substring(0, KeyName.Length + 1).ToLower = KeyName.ToLower + "=" Then
                        Return Convert.ToInt32(Right(FileName_Strings(i), FileName_Strings(i).Length - (KeyName.Length + 1)))
                    End If
                Next
                Return nDefault
            End If
        End If
        Return -1
    End Function
End Module