Being that this application has evolved over the years, there are still some INI files. I have a class that reads entries using GetPrivateProfileString.
At the top of the class we see this:
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key, string def, StringBuilder retVal,
int size, string filePath);
And it looks like there is a public method that looks something like this:
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();
}
I recently noticed that GetPrivateProfileString trims it’s data. Therefore, if my INI file has an entry like this:
SomeData= Notice the three trailing spaces at front and back of this sentence.
It will retrieve it like (notice that it’s trimmed to the left and right – ignore quotes):
“Notice the three trailing spaces at front and back of this sentence.”
I don’t want it to Trim. Is that out of my control? INI files aren’t allowed to have spaces after the equal sign (e.g. SomeData=)?
As pointed out in the comments, that is how the API works. If you can live with that, you can at least save some DllImport work by using for example this library/wrapper (includes source, just one file):
IniReader