I am trying to write c# code to read key value from an ini file which doesnot contain a section header. As
private static extern int GetPrivateProfileString(string section, string key,string def, StringBuilder retVal,int size,string filePath);
function does not work in this case(as mentioned here) ,I tried to create another ini file with section header inserted . Then the GetPrivateProfileString function was used to read the key value. The ini file is getting created as expected, but the function is giving null value as result. Where have i gone wrong?
The code snippet is given below
//someFilePath contains .ini file
string userFilePath = "someFilePath";
string sectionName = "TempSectionHeader";
string copyFile = "text.ini";
if (File.Exists(userFilePath))
{
if(File.Exists(copyFile))
{
File.Delete(copyFile);
}
File.AppendAllText("text.ini", "["+ sectionName + "]");
string contents = File.ReadAllText(userFilePath);
contents = contents.Replace("\0", "");
File.AppendAllText(copyFile, "\r\n");
File.AppendAllText(copyFile, contents);
installName = ReadValueFromINIFile(sectionName, "installName", copyFile);
MessageBox.Show(installName);
}