2

由于这个应用程序已经发展了多年,仍然有一些 INI 文件。我有一个使用 GetPrivateProfileString 读取条目的类。

在类的顶部,我们看到:

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

看起来有一个公共方法看起来像这样:

    public string IniReadValue(string Section, string Key)
    {
        // If string greater than 254 characters (255th spot is null-terminator),
        // string will be truncated.
        const int capacity = 255;
        StringBuilder temp = new StringBuilder(capacity);
        int i = GetPrivateProfileString(Section, Key, "", temp,
                                        capacity, this.m_Path);
        return temp.ToString();
    }

我最近注意到 GetPrivateProfileString 修剪它的数据。因此,如果我的 INI 文件有这样的条目:

SomeData= 注意这句话前后的三个空格。

它会像这样检索它(注意它被修剪到左右 - 忽略引号):

“注意这句话前后的三个空格。”

我不希望它修剪。这是我无法控制的吗?INI 文件不能在等号后有空格(例如 SomeData=)?

4

2 回答 2

2

正如评论中所指出的,这就是 API 的工作方式。如果你能忍受,你至少可以通过使用这个库/包装器(包括源代码,只有一个文件)来保存一些 DllImport 工作:

阅读器

于 2012-11-26T21:42:55.663 回答
2

您可以使用引号来表达您的内容,当将内容读入字符串时,您可以轻松解析出您想要的内容。像这样:key =“内容”

您可以在函数 IniReadValue 中添加一些代码。

或者您可以使用 base64 字符串放置/获取消息,如下所示: some-key = your-content-in-base64-string 并且许多字符问题不会是您的问题。但是这种方式不利于阅读。

于 2012-11-27T09:47:05.553 回答