//读取ini文件例子
Config.ini文件内容:
[ComSet]
Port=COM3
Rate=2400
//程序内容
BOOL PortCom::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CFileFind find;
if (!find.FindFile("Config.ini"))//查找Config.ini文件是否存在
{
AfxMessageBox("配置文件丢失,重新配置串口设置!");
comSet comSetDlg;
comSetDlg.DoModal();
}
find.Close();
char strBuff[256];
int returnnumber,returnnumber1;
CString strValue = _T(""),strValue1 = _T("");
CString strFilePath;
GetCurrentDirectory(256,strBuff); //获取当前路径[lu jing]
strFilePath.Format("%s//Config.ini",strBuff);
returnnumber=GetPrivateProfileString("ComSet","Port",NULL,strBuff,80,strFilePath); //读取Config.ini文件[ComSet]中相应字段[Port]的内容
strValue=strBuff;
returnnumber1=GetPrivateProfileString("ComSet","Rate",NULL,strBuff,80,strFilePath); //读取Config.ini文件[ComSet]中相应字段[Rate]的内容
strValue1=strBuff;
//AfxMessageBox(strValue+strValue1);
if(returnnumber<=0)
{
AfxMessageBox("配置文件Config.ini的字段读取错误,请选择好端口信息,并保存重建Config.ini文件!");
}
else
{
//AfxMessageBox("读取配置文件Config.ini完成!");
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}