// 设置配置文件名字
SetRegistryKey(_T("MyApp"));
// 读取配置文件
CString strValue;
CWinApp* pApp = AfxGetApp();
if (pApp->GetProfileString(_T("Settings"), _T("Value"), strValue))
{
// 读取成功,处理配置值
}
else
{
// 读取失败,配置值不存在或出错
}
// 写入配置文件
CString strValue = _T("Hello, World!");
CWinApp* pApp = AfxGetApp();
pApp->WriteProfileString(_T("Settings"), _T("Value"), strValue);
附录:
// 将配置文件设置到执行文件的同一目录下
TCHAR SetFile[MAX_PATH]{};
int GetExcutionFileName_result = GetModuleFileName(m_hInstance, SetFile, MAX_PATH);
if (GetExcutionFileName_result) {
SetFile[GetExcutionFileName_result - 3] = 'i';
SetFile[GetExcutionFileName_result - 2] = 'n';
SetFile[GetExcutionFileName_result - 1] = 'i';
if (m_pszProfileName) {
free((void*)m_pszProfileName);
}
//m_pszProfileName = (LPCTSTR)_strdup((_bstr_t)SetFile);
m_pszProfileName = (LPCTSTR)_tcsdup(SetFile);