使用CWinApp类的WriteProfileString 、GetProfileString等函数。
1、如果不调用SetRegistryKey(),CWinApp 会把信息保存系统目录的的Test.ini文件中(C:\Windows\Test.ini)
2、如果调用SetRegistryKey(<company name>),CWinApp 会把信息保存系统目录的的注册表中
HKEY_CURRENT_USER\Software\<company name>\Test\<section name>\<value name>.
注:SetRegistryKey()是CWinApp的保护成员,只能被CWinApp的成员函数调用。而WriteProfileString 、GetProfileString等函数则是公共成员,可以随便调用,一般调用形式如:
theApp->WriteProfileString(); //在Test.cpp中。
AfxGetApp()->WriteProfileString(); 在其它源文件中
HKEY_CURRENT_USER\Software\Local AppWizard-Generated Applications\Msg\A
AfxGetApp()->WriteProfileString(L"A",L"B",L"V");
CString set = AfxGetApp()->GetProfileString(L"A",L"B");
在利用mfc框架的时候,在App应用类的InitInstance()函数中,初始化时总有一个 SetRegistryKey("字符串XXX")。如果你使用注册表保存相关参数,则它为你提供了很便利的方法,当然如果不使用系统注册表,这句可以注释掉。
SetRegistryKey 这个函数功能是设置MFC程序的注册表访问键,并把读写 ini 文件的成员函数映射到读写注册表。只要调用一下 SetRegistryKey 并指定注册表键值,那么下面6个成员函数,就被映射到进行注册表读取了~
WriteProfileBinary | Writes binary data to an entry in the application's .INI file. |
WriteProfileInt | Writes an integer to an entry in the application's .INI file. |
WriteProfileString | Writes a string to an entry in the application's .INI file. |
GetProfileBinary | Retrieves binary data from an entry in the application's .INI file. |
GetProfileInt | Retrieves an integer from an entry in the application's .INI file. |
GetProfileString | Retrieves a string from an entry in the application's .INI file. |
废话少说,举例如下:
BOOL COLContactFinal2005App::InitInstance()
{
SetRegistryKey(_T("COLContact "));
WriteProfileString(_T("OLTest"),_T("Test1"),_T("abc"));
}
执行上述代码将在注册表生成如下键值:
[HKEY_CURRENT_USER\Software\COLContact \OLContactFinal2005\OLTest]
"Test1"="abc"
过SetRegistrykey | 导致应用程序设置,而不是存储在注册表中。INI文件。 |
名SetRegistryKey这个函数功能是设置MFC程序的注册表访问键,并把读写INI文件的成员函数映射到读写注册表。只要调用一下名SetRegistryKey并指定注册表键值,那么下面6个成员函数,就被映射到进行注册表读取了〜
WriteProfileBinary | 写入二进制数据的应用程序的INI文件中的条目。 |
WriteProfileInt | 整数写入应用程序的INI文件中的条目。 |
WriteProfileString | 将一个字符串写入到应用程序的INI文件中的条目。 |
GetProfileBinary | 检索二进制数据从应用程序的INI文件中的一个条目。 |
GetProfileInt | 从应用程序的INI文件中的一个条目检索一个整数。 |
GetProfileString | 从应用程序的INI文件中的一个条目检索字串。 |
应用程序“)); / /这里是准备在注册表HKEY_CURRENT_USER / /软件下面生成一个薄利的应用程序strUserName中,strPassword WriteProfileString(的“LOGINFO”,“用户名”,strUserName中)在/ /向注册表HKEY_CURRENT_USER / /软件/ /勃利的应用程序/ / / /分支下写入用户名字符串行键值〜WriteProfileString(“LOGINFO LOGINFO “,”密码“,strPassword);/ /同上~~ strUserName中GetProfileString(的“LOGINFO”,“用户名”);/ /这里是读取HKEY_CURRENT_USER / /软件/ /勃利的应用程序/ / / / LOGINFO分支下的用户名字符串键值到strUserName中〜 strPassword = GetProfileString(的“LOGINFO”,“密码”);在如果不是在CWinApp的派生的类中读写注册表,可以直接用: strUserName中theApp.GetProfileString(的“LOGINFO”,“用户名”) strPassword = theApp.GetProfileString(的“LOGINFO”,“密码”); 或 strUserName中= AfxGetApp() - > GetProfileString(的“LOGINFO”,“用户名”); 条条大路通罗马。