Save file settings in ini instead of registry

2019-07-15 17:24发布

I'm new to MFC, once I create my first app, in myApp::InitInstance() . I have

SetRegistryKey(_T("Local AppWizard-Generated Applications"));

Can I delete this and save settings to my own ini construct ?

标签: mfc
4条回答
冷血范
2楼-- · 2019-07-15 17:36

Use win32 APIs WriteProfileString (write to INI file) and GetProfileString (read from INI file) For more help ms-help://MS.MSDNQTR.v90.en/sysinfo/base/writeprofilestring.htm

查看更多
淡お忘
3楼-- · 2019-07-15 17:43

Yes you can. CWinApp::SetProfileXXX() does this for you, actually - but I wouldn't use these methods anymore in 2010, they were OK when ppl moved from .ini to the registry.

查看更多
倾城 Initia
4楼-- · 2019-07-15 17:53

I am not sure if this is possible as a .ini file has only strings for your program. You can create an operating system script (.bat for windows, .sh for unix etc) and call it using system() call.

查看更多
Explosion°爆炸
5楼-- · 2019-07-15 17:56

Edit: 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.


To store values in an .ini file instead of the registry:

  1. Omit the call to SetRegistryKey.
  2. In your app class, set m_pszProfileName to the full path of your .ini file. The filename string must be allocated using malloc, because the framework will call free on it when your app shuts down. First free the existing value, then assign your new string:

    free((void*)m_pszProfileName);
    m_pszProfileName = ::_tcsdup(_T("C:\\somedir\\myini.ini"));

  3. Call CWinApp::GetProfileInt, CWinApp::WriteProfileInt and similar functions as usual.

I strongly recommend using a path under APPDATA for storing your .ini file.

查看更多
登录 后发表回答