I am trying to read foreign characters from an .ini file. This is the method I am using
[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string Section,
int Key,
string Value,
[MarshalAs(UnmanagedType.LPArray)] byte[] Result,
int Size,
string FileName);
public static string[] GetEntryNames(string section, string iniPath)
{
for (int maxsize = 500; true; maxsize *= 2)
{
byte[] bytes = new byte[maxsize];
int size = Imports.GetPrivateProfileString(section, 0, "", bytes, maxsize, iniPath);
if (size < maxsize - 2)
{
string entries = Encoding.ASCII.GetString(bytes, 0,
size - (size > 0 ? 1 : 0));
Console.WriteLine("Entries: " + entries.Split(new char[] { '\0' })[3]);
return entries.Split(new char[] { '\0' });
}
}
}
I am using Encoding.ASCII
but apparently GetPrivateProfileString
isn't. The bytes coming out of it need encoding probably. How can I do that?
Edit: Example
This will print : Tavar? instead of Tavaré
To read text that contains french accents, you usually want to use one of the following encodings :
Encoding.UTF8
Encoding.GetEncoding("iso-8859-1")
Encoding.GetEncoding("windows-1252")
Edit: These worked for OP :
Encoding.UTF7
Encoding.GetEncoding("iso-8859-1")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With