将文件设置保存在ini而不是注册表中 [英] Save file settings in ini instead of registry

查看:104
本文介绍了将文件设置保存在ini而不是注册表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦我在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:

  1. 忽略对SetRegistryKey的呼叫.
  2. 在您的应用程序类中,将m_pszProfileName设置为.ini文件的完整路径.文件名字符串必须使用malloc进行分配,因为当您的应用关闭时,框架将在其上调用free.首先free现有值,然后分配您的新字符串:

  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"));

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

调用CWinApp::GetProfileIntCWinApp::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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆