将文件设置保存在ini而不是注册表中 [英] Save file settings in ini instead of registry
问题描述
一旦我在myApp :: InitInstance()中创建了第一个应用程序,我对MFC还是陌生的.我有
I'm new to MFC, once I create my first app, in myApp::InitInstance() . I have
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
我可以删除此设置并将设置保存到自己的ini结构中吗?
Can I delete this and save settings to my own ini construct ?
推荐答案
编辑:经过进一步测试,如果您的应用类是从CWinAppEx
派生的,则以下解决方案将不起作用!如果您的应用是直接从CWinApp
派生的,它确实可以工作.
After further testing, the solution below does not work if your app class is derived from CWinAppEx
! It does work if your app is directly derived from CWinApp
.
要将值存储在.ini
文件而不是注册表中:
To store values in an .ini
file instead of the registry:
- 忽略对
SetRegistryKey
的呼叫. -
在您的应用程序类中,将
m_pszProfileName
设置为.ini
文件的完整路径.文件名字符串必须使用malloc
进行分配,因为当您的应用关闭时,框架将在其上调用free
.首先free
现有值,然后分配您的新字符串:
- Omit the call to
SetRegistryKey
. In your app class, set
m_pszProfileName
to the full path of your.ini
file. The filename string must be allocated usingmalloc
, because the framework will callfree
on it when your app shuts down. Firstfree
the existing value, then assign your new string:
free((void*)m_pszProfileName);
m_pszProfileName = ::_tcsdup(_T("C:\\somedir\\myini.ini"));
free((void*)m_pszProfileName);
m_pszProfileName = ::_tcsdup(_T("C:\\somedir\\myini.ini"));
调用CWinApp::GetProfileInt
,CWinApp::WriteProfileInt
和与往常相似的功能.
Call CWinApp::GetProfileInt
, CWinApp::WriteProfileInt
and similar functions as usual.
我强烈建议在APPDATA下使用一个路径来存储您的.ini
文件.
I strongly recommend using a path under APPDATA for storing your .ini
file.
这篇关于将文件设置保存在ini而不是注册表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!