I have one project with function to read .ini files. I can not display the contents of .ini file that I want to.
My code to read .ini file
Public Fun
Those suggested Windows API have been deprecated for a long time now and they are really bad, you see on each GetPrivateProfile call you are reading and parsing the INI content all over again...
As an alternative try using my MadMilkman.Ini library, like this:
Imports MadMilkman.Ini
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ini As New IniFile()
ini.Load("D:\WorldInfo.ini")
Label1.Text = ini.Sections("B_Empty_IndexList").Keys("Count").Value
Label2.Text = ini.Sections("B_Use_IndecList").Keys("Count").Value
End Sub
You can download the library from here: https://github.com/MarioZ/MadMilkman.Ini
EDIT
I was asked to elaborate more the discussion that I had with IInspectable in the comments below.
IInspectable: "Your solution doesn't appear to account for surrogate pairs."
Mario Z: "I have no idea why you assume that library doesn't support Unicode, it does and its quite simple... just use the appropriate encoding."
So here it goes, in short .NET's String represents an array of Chars and Char represents 16-bit character. When faced with a string that contains a 32-bit character it will use a so called "surrogate pair" (two characters that represent one). Now when working with this kind of string an issue that can happen for example is that we can make an invalid substring of that string if we cut it in the middle of that "surrogate pair". Also another issue that can happen is when working with the string indexer and not taking into account that a "surrogate pair" will consist of two indexed chars in that string.
However that is all not the case with MadMilkman.Ini, the library directly manipulates only with a specific set of characters while the rest of the string is left as it is (string is a self-consistent type with a full Unicode support). The characters that are targeted and manipulated are [, ], =, etc.
As an example here is a writing test sample:
Dim textWithSurrogatePairs =
"sample content " + Char.ConvertFromUtf32(Int32.Parse("22222", NumberStyles.HexNumber))
Dim ini = New IniFile(
New IniOptions() With {.Encoding = Encoding.Unicode})
ini.Sections.Add(
New IniSection(ini, "sample section",
New IniKey(ini, "sample key", textWithSurrogatePairs)))
ini.Save("sample file.ini")
The following is the content of "textWithSurrogatePairs" variable:
The following is the generated output "sample file.ini" file:
Also here is reading test sample:
Dim ini = New IniFile(
New IniOptions() With {.Encoding = Encoding.Unicode})
ini.Load("sample file.ini")
Dim readValue = ini.Sections("sample section").Keys("sample key").Value
The following is the "readValue" variable:
So in short the .NET framework itself handles the surrogate pairs, the only thing that we need to be aware of is to use an appropriate Encoding (as shown above).
Unfortunately this is something that IInspectable fails to realize and I failed to properly explain to him.