WritePrivateProfileString - is unpredictable

后端 未结 2 1210
夕颜
夕颜 2021-01-16 02:41

We are using a tool that uses internally the simple Win-Api call WritePrivateProfileString.

I\'m aware of the define flag UNICODE to map either to

2条回答
  •  无人及你
    2021-01-16 03:07

    1. Always use WritePrivateProfileStringW version.

       [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true,
       EntryPoint = "WritePrivateProfileStringW")]
       [return: MarshalAs(UnmanagedType.Bool)]
       public static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
      
    2. When the INI files does not exist, create it with Unicode file marker (first two bytes FF FE). By this way all strings are stored as Unicode. To create a blank ini file you can use this:

       File.WriteAllText(ini_file_path, "; Your comment\r\n", Encoding.Unicode);
      

    A blank/comment line at the beginning is recommended. Write at least "\r\n".

提交回复
热议问题