如果path包含特殊字符,则GetPrivateProfileString不起作用

时间:2015-10-05 09:07:21

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

我使用以下代码在c#framework.net 2.0中创建了一个dll:

// Translate username and password to TFS Credentials
ICredentials networkCredential = new NetworkCredential(tfsUsername, tfsPassword, domain);
WindowsCredential windowsCredential = new WindowsCredential(networkCredential);
TfsClientCredentials tfsCredential = new TfsClientCredentials(windowsCredential, false);

// Connect to TFS Work Item Store
Uri tfsUri = new Uri(@"http://my-server:8080/tfs/DefaultCollection");
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(tfsUri, tfsCredential);
WorkItemStore witStore = new WorkItemStore(tfs);

该函数声明如下:

/// <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();
}

如果我创建一个调用此代码的winform项目,如果ini文件位于文档文件夹中,并且用户有一个特殊的字符,如[DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); ç,那么也可以正常工作。

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

任何想法?

1 个答案:

答案 0 :(得分:2)

使用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);