对INI文件的函数进行了封装,如果默认值不存在则创建默认值项目。
#if ! defined AFX_INI
#define AFX_INI
void DeleteSection(CString OpenFileName,CString Section) //删除INI文件中的块
{
WritePrivateProfileString(Section, NULL, NULL, OpenFileName);
return;
}
CString GetIniS(CString OpenFileName, CString SectionName,CString KeyWord, CString DefString) //读取ini文件值
{
CString ResultString;
char Temp[256];
long LengthTemp;
LengthTemp=GetPrivateProfileString(SectionName, KeyWord, "",Temp,256, OpenFileName);
if (LengthTemp==0)
{
WritePrivateProfileString(SectionName, KeyWord, DefString, OpenFileName);
ResultString=DefString;
}
else
{
ResultString=Temp;
}
return ResultString;
}
long GetIniN(CString OpenFileName,CString SectionName,CString KeyWord,long DefValue)//读取ini文件值
{
char Temp[256];
long v;
CString s;
v=GetPrivateProfileInt(SectionName, KeyWord, DefValue, OpenFileName);
if(GetPrivateProfileString(SectionName, KeyWord, "",Temp,256, OpenFileName)==0)
{
s.Format("%ld",DefValue);
WritePrivateProfileString(SectionName, KeyWord, s, OpenFileName);
}
return v;
}
void SetIniS(CString OpenFileName, CString SectionName,CString KeyWord, CString ValString) //设置ini文件值
{
WritePrivateProfileString(SectionName, KeyWord, ValString, OpenFileName);
return;
}
void SetIniN(CString OpenFileName,CString SectionName,CString KeyWord,long SetValue)//设置ini文件值
{
CString Str;
Str.Format("%ld",SetValue);
WritePrivateProfileString(SectionName, KeyWord, Str, OpenFileName);
return;
}
#endif