Reading from an INI File

It is very easy for me to write to an INI file, but I have some problems extracting data from an already created INI file.

I am using this function:

Public 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 

If I have an INI file named "c: \ temp \ test.ini" with the following data:

 [testApp] KeyName=keyValue KeyName2=keyValue2 

How can I get the values ​​of KeyName and KeyName2?

I tried this code without success:

  Dim strData As String GetPrivateProfileString("testApp", "KeyName", "Nothing", strData, Len(strData), "c:\temp\test.ini") MsgBox(strData) 
+4
source share
1 answer

Going to the Pinvoke.Net website and changing its working example, their function announcement is different.

Modified Example

 Imports System.Runtime.InteropServices Imports System.Text Module Module1 Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (ByVal lpAppName As String, _ ByVal lpKeyName As String, _ ByVal lpDefault As String, _ ByVal lpReturnedString As StringBuilder, _ ByVal nSize As Integer, _ ByVal lpFileName As String) As Integer Sub Main() Dim res As Integer Dim sb As StringBuilder sb = New StringBuilder(500) res = GetPrivateProfileString("testApp", "KeyName", "", sb, sb.Capacity, "c:\temp\test.ini") Console.WriteLine("GetPrivateProfileStrng returned : " & res.ToString()) Console.WriteLine("KeyName is : " & sb.ToString()) Console.ReadLine(); End Sub End Module 
+6
source

All Articles