【问题标题】:GetPrivateProfileString doesn't work if path contains special chars如果路径包含特殊字符,GetPrivateProfileString 不起作用
【发布时间】:2016-01-01 21:16:20
【问题描述】:

我用这段代码在 c# framework.net 2.0 中创建了一个 dll:

/// <summary>
/// Read Data Value From the Ini File
/// </summary>
/// <PARAM name="Section"></PARAM>
/// <PARAM name="Key"></PARAM>
/// <PARAM name="Path"></PARAM>
/// <returns></returns>
public string IniReadValue(string Section, string Key)
{
    StringBuilder temp = new StringBuilder(255);
    int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
    return temp.ToString();
}

函数声明如下:

[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

如果我创建一个调用此代码的 winform 项目也可以正常工作,如果 ini 文件位于文档文件夹中并且用户具有特殊字符,例如 çД

这个 dll 我需要在 unity3d 中使用,并且工作正常,但是如果文件夹包含上面的特殊字符 (example) 它不起作用并返回一个空字符串..

有什么想法吗?

【问题讨论】:

  • 你是如何声明这个函数的?
  • @RezaAghaei 在问题中添加了声明:)
  • 你真的需要这个功能吗?这是旧的,已弃用的原生函数调用 (stackoverflow.com/questions/7336185/…),为什么不手动读取 ini?
  • 感谢@Skyblade,但手动读取ini 是什么意思?
  • @ghiboz 我的意思是使用你自己的代码。这并不难,您可以搜索现有的实现。

标签: c# winforms unity3d .net-2.0 class-library


【解决方案1】:

使用CharSet=CharSet.Unicode:

[DllImport("kernel32", CharSet=CharSet.Unicode)]
private static extern int GetPrivateProfileString(string section,
             string key, string def, StringBuilder retVal,
             int size, string filePath);

【讨论】:

    猜你喜欢
    • 2021-10-15
    • 1970-01-01
    • 2013-06-05
    • 2021-01-08
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    相关资源
    最近更新 更多