Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As String, ByVal nSize As Int32, _
ByVal lpFileName As String) As Int32
Public Overloads Sub ReadINIFile(ByVal INIPath As String, _
ByVal SectionName As String, ByVal KeyName As String(), _
ByRef KeyValue As String())
Dim Length As Integer
Dim strData As String
strData = Space$(1024)
For i As Integer = 1 To KeyName.Length - 1
If KeyName(i) <> "" Then
'This will read the ini file using Section Name and Key
Length = GetPrivateProfileString(SectionName, KeyName(i), KeyValue(i), _
strData, strData.Length, LTrim(RTrim((INIPath))))
If Length > 0 Then
KeyValue(i) = strData.Substring(0, Length)
Else
KeyValue(i) = ""
End If
End If
Next
End Sub
Testing above function
Create a Sample INI file using below text in C:\Logs\test.ini
[Sample Section 1]
SampleKey1=SAMPLEVALUE1
SampleKey2=SAMPLEVALUE2
[Sample Section 2]
SampleKey3=SAMPLEVALUE3
SampleKey4=SAMPLEVALUE4
And use following code
Dim KeyName(2) As String
Dim KeyValues(2) As String
KeyName(1) = "ServerName"
KeyName(2) = "DBName"
ReadINIFile("C:\Logs\test.ini", "TEST Server", KeyName, KeyValues)
MessageBox.Show(KeyValues(1) & " " & KeyValues(2))
Hope this helps
No comments:
Post a Comment