0

I save a parameter (a Persian text) manually to .ini File. Now I want to read it in C# (WPF) using this Code:

[DllImport("kernel32", CharSet = CharSet.Unicode)]
public static extern int GetPrivateProfileString(
    string Section, 
    string Key, 
    string Default, 
    StringBuilder RetVal, 
    int Size, 
    string FilePath);

When I want to show it using MessageBox, I get some un-known characters. Even I tried this code for encoding (with different type of encoding):

        var enc = Encoding.Default;
        var bytes = enc.GetBytes(_address);
        System.Windows.MessageBox.Show(
            _address);

but I still see, unknown character. _address is aforementioned Persian parameter, which as retrieved using GetPrivateProfileString method (as retVale). I note that I can see the string is saved normally (in NotePad with UTF-8). How can I fix this problem?

Babak.Abad
  • 2,839
  • 10
  • 40
  • 74
  • how are you setting _address? – Stefan Dec 07 '19 at 21:10
  • Don't you know what encoding was used to store that string? Did you use `WritePrivateProfileStringW`? Have you tried `Encoding.Unicode` or `Encoding.UTF8`? – Jimi Dec 07 '19 at 21:13
  • @Stefan. `_address` is aforementioned Persian parameter, which as retrieved using `GetPrivateProfileString` method (as retVale). – Babak.Abad Dec 07 '19 at 21:15
  • Btw, you can read the BOM of that file with a hex editor (or a StreamReader): Unicode (UTF16-LE) BOM is `FF FE`, UTF8, if present, is `EF BB BF`. – Jimi Dec 07 '19 at 21:21
  • Also, you can read the chars instead of a string, changing `StringBuilder RetVal` in `[In, Out] char[] RetVal` – Jimi Dec 07 '19 at 21:31
  • Use a native C# solution such as https://github.com/rickyah/ini-parser – VahidN Dec 08 '19 at 04:19

0 Answers0