由于这个应用程序已经发展多年,还有一些INI文件。我有一个使用GetPrivateProfileString读取条目的类。GetPrivateProfileString without Trim?
当时我们看到这个类的顶部:
[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 =注意这句话前后三个尾随空格。
它将检索它像(请注意,它修剪到左,右 - 忽略引号):
“注意在前面,这句话的后面3个连续的空间。”
我不希望它修剪。这是不是我的控制? INI文件在等号后不允许有空格(例如SomeData =)?
你可以随时制作自己的解析器 –
@Sam我。是的我同意。我也很好奇这个特殊的功能,以及为什么它需要修剪字符串。尽管感谢您的意见,但我已经对您的意见投了赞成票。 – JustLooking
当需要前导或尾随空格时,在ini文件中使用双引号。 –