我是在使用INI文件时,在MSDN中发现的这些信息。
虽然很简单,且有抄袭的嫌疑,不过也许对有些同志(志同道合者)有用。
MFC类CWinApp提供有标准的注册表操作代码

一、设置注册表的起始位置
Causes application settings to be stored in the registry instead of INI files.void SetRegistryKey(LPCTSTR lpszRegistryKey);
void SetRegistryKey(UINT nIDRegistryKey);

Parameters
lpszRegistryKey  Pointer to a string containing the name of the key. 
nIDRegistryKey       ID/index of a key in the registry. 

Res
This function sets m_pszRegistryKey, which is then used by the GetProfileInt, GetProfileString, WriteProfileInt, and WriteProfileString member functions of CWinApp. If this function has been called, the list of most recently-used (MRU) files is also stored in the registry. The registry key is usually the name of a company. It is stored in a key of the following form: HKEY_CURRENT_USER\Software\<company name>\<application name>\<section name>\<value name>.

与SetRegistryKey相关的公共成员变量
Used to determine where, in the registry or INI file, application profile settings are stored.LPCTSTR m_pszRegistryKey;Res
Normally, this data member is treated as read-only. Registry entries are stored as follows: In Windows NT, the value is stored to a registry key. The name for the application profile setting is appended to the following registry key: HKEY_CURRENT_USER/Software/LocalAppWizard-Generated/. 
In Windows 3.x, the value is stored in the WIN.INI file. 
In Windows 95/98, the value is stored in a cached version of WIN.INI. 
If you assign a value to m_pszRegistryKey, it must be dynamically allocated on the heap. The CWinApp destructor calls free( ) with this pointer. You many want to use the _tcsdup( ) run-time library function to do the allocating. Also, free the memory associated with the current pointer before assigning a new value. For example://First free the string allocated by MFC at CWinApp startup.
//The string is allocated before InitInstance is called.
free((void*)m_pszRegistryKey);
//Change the name of the registry key.
//The CWinApp destructor will free the memory.
m_pszRegistryKey=_tcsdup(_T("HKEY_CURRENT_USER\\Software\\mycompany\\myapp\\thissection\\thisvalue"));二、设置Profile层次
与其相关的公共成员变量
Contains the name of the application's .INI file.LPCTSTR m_pszProfileName;
Res
m_pszProfileName is a public variable of type const char*.Note   If you assign a value to m_pszProfileName, it must be dynamically allocated on the heap. The CWinApp destructor calls free( ) with this pointer. You many want to use the _tcsdup( ) run-time library function to do the allocating. Also, free the memory associated with the current pointer before assigning a new value. For example:
//First free the string allocated by MFC at CWinApp startup.
//The string is allocated before InitInstance is called.
free((void*)m_pszProfileName);
//Change the name of the .INI file.
//The CWinApp destructor will free the memory.
m_pszProfileName=_tcsdup(_T("d:\\somedir\\myini.ini"));

解决方案 »

  1.   

    三、读取键值
    ===========================
    CWinApp::GetProfileBinary()
    ===========================
    Call this member function to retrieve binary data from an entry within a specified section of the application's registry or .INI file.BOOL GetProfileBinary(
       LPCTSTR lpszSection,
       LPCTSTR lpszEntry,
       LPBYTE* ppData,
       UINT* pBytes 
    );
    Parameters
    lpszSection 
    Points to a null-terminated string that specifies the section containing the entry. 
    lpszEntry 
    Points to a null-terminated string that contains the entry whose value is to be retrieved. 
    ppData 
    Points to a pointer that will receive the address of the data. 
    pBytes 
    Points to a UINT that will receive the size of the data (in bytes). 
    Return Value
    Nonzero if successful; otherwise 0.Res
    The entries are stored as follows: In Windows NT, the value is stored to a registry key. 
    In Windows 3.x, the value is stored in the WIN.INI file. 
    In Windows 95/98, the value is stored in a cached version of WIN.INI. 
    This member function is not case sensitive, so the strings in the lpszSection and lpszEntry parameters may differ in case.Note   GetProfileBinary allocates a buffer and returns its address in *ppData. The caller is responsible for freeing the buffer using delete [].
    Example
    BOOL CMyApp::InitInstance()
    {
        // CMyApp is derived from CWinApp.    const char *pszKey = "MyApp";
        struct complex {
            double re, im;
        } myData = { 1.4142, -0.5 };    // Change the registry key under which our settings are stored.    SetRegistryKey(_T(""));    // Write the information to the registry.    WriteProfileBinary(pszKey, "ComplexData", (LPBYTE)&myData, sizeof(myData) );    // Read the information from the registry.    complex* pData;
        UINT n;
        BOOL ret = GetProfileBinary(pszKey, "ComplexData", (LPBYTE*)&pData, &n);    ASSERT(ret);
        ASSERT(n == sizeof(complex));
        ASSERT(myData.re == pData->re);
        ASSERT(myData.im == pData->im);
        delete [] pData; // free the buffer
        return TRUE;
    }========================
    CWinApp::GetProfileInt()
    ========================
    Call this member function to retrieve the value of an integer from an entry within a specified section of the application's registry or .INI file.UINT GetProfileInt(
       LPCTSTR lpszSection,
       LPCTSTR lpszEntry,
       int nDefault 
    );
    Parameters
    lpszSection 
    Points to a null-terminated string that specifies the section containing the entry. 
    lpszEntry 
    Points to a null-terminated string that contains the entry whose value is to be retrieved. 
    nDefault 
    Specifies the default value to return if the framework cannot find the entry. This value can be an unsigned value in the range 0 through 65,535 or a signed value in the range –32,768 through 32,767. 
    Return Value
    The integer value of the string that follows the specified entry if the function is successful. The return value is the value of the nDefault parameter if the function does not find the entry. The return value is 0 if the value that corresponds to the specified entry is not an integer.This member function supports hexadecimal notation for the value in the .INI file. When you retrieve a signed integer, you should cast the value into an int.Res
    The entries are stored as follows: In Windows NT, the value is stored to a registry key. 
    In Windows 3.x, the value is stored in the WIN.INI file. 
    In Windows 95/98, the value is stored in a cached version of WIN.INI. 
    This member function is not case sensitive, so the strings in the lpszSection and lpszEntry parameters may differ in case.Example
    BOOL CMyApp::InitInstance()
    {
        // CMyApp is derived from CWinApp.    const char *pszKey = "MyApp";
        const char *pszName = "Julian";
        int iAge = 26;    // Change the registry key under which our settings are stored.    SetRegistryKey(_T(""));    // Write the information to the registry.    WriteProfileString(pszKey, "Name", pszName);
        WriteProfileInt(pszKey, "Age", iAge);    // Read the information from the registry.    CString strName = GetProfileString(pszKey, "Name");
        int iAge2 = GetProfileInt(pszKey, "Age", 0);    ASSERT(strName == pszName);
        ASSERT(iAge2 == iAge);    return TRUE;
    }===========================
    CWinApp::GetProfileString()
    ===========================
    Call this member function to retrieve the string associated with an entry within the specified section in the application's registry or .INI file.CString GetProfileString(
       LPCTSTR lpszSection,
       LPCTSTR lpszEntry,
       LPCTSTR lpszDefault = NULL 
    );
    Parameters
    lpszSection 
    Points to a null-terminated string that specifies the section containing the entry. 
    lpszEntry 
    Points to a null-terminated string that contains the entry whose string is to be retrieved. This value must not be NULL. 
    lpszDefault 
    Points to the default string value for the given entry if the entry cannot be found in the initialization file. 
    Return Value
    The return value is the string from the application's .INI file or lpszDefault if the string cannot be found. The maximum string length supported by the framework is _MAX_PATH. If lpszDefault is NULL, the return value is an empty string.Res
    The entries are stored as follows: In Windows NT, the value is stored to a registry key. 
    In Windows 3.x, the value is stored in the WIN.INI file. 
    In Windows 95/98, the value is stored in a cached version of WIN.INI. 
    Example
       CString strSection       = "My Section";
       CString strStringItem    = "My String Item";
       CString strIntItem       = "My Int Item";   CWinApp* pApp = AfxGetApp();   pApp->WriteProfileString(strSection, strStringItem, "test");   CString strValue;
       strValue = pApp->GetProfileString(strSection, strStringItem);
       ASSERT(strValue == "test");   pApp->WriteProfileInt(strSection, strIntItem, 1234);
       int nValue;
       nValue = pApp->GetProfileInt(strSection, strIntItem, 0);
       ASSERT(nValue == 1234);
      

  2.   

    四、改写键值
    =============================
    CWinApp::WriteProfileBinary()
    =============================
    Call this member function to write binary data into the specified section of the application's registry or .INI file.BOOL WriteProfileBinary(
       LPCTSTR lpszSection,
       LPCTSTR lpszEntry,
       LPBYTE pData,
       UINT nBytes 
    );
    Parameters
    lpszSection 
    Points to a null-terminated string that specifies the section containing the entry. If the section does not exist, it is created. The name of the section is case independent; the string may be any combination of uppercase and lowercase letters. 
    lpszEntry 
    Points to a null-terminated string that contains the entry into which the value is to be written. If the entry does not exist in the specified section, it is created. 
    pData 
    Points to the data to be written. 
    nBytes 
    Contains the number of bytes to be written. 
    Return Value
    Nonzero if successful; otherwise 0.Res
    The entries are stored as follows: In Windows NT, the value is stored to a registry key. 
    In Windows 3.x, the value is stored in the WIN.INI file. 
    In Windows 95/98, the value is stored in a cached version of WIN.INI. 
    Example
    This example uses CWinApp* pApp = AfxGetApp(); to get at the CWinApp class illustrating a way that WriteProfileBinary and GetProfileBinary can be used from any function in an MFC application.CString strSection = "My Section";
    CString strItem = "My Binary Item";
    double myData = 123.456e12;CWinApp* pApp = AfxGetApp();pApp->WriteProfileBinary(strSection, strItem, (LPBYTE)&myData, sizeof(myData));
    double *pData;
    UINT n;
    pApp->GetProfileBinary(strSection, strItem, (LPBYTE*)&pData, &n );
    ASSERT( n == sizeof(myData) );
    ASSERT( myData = *pData );
    delete [] pData;  // free the buffer==========================
    CWinApp::WriteProfileInt()
    ==========================
    Call this member function to write the specified value into the specified section of the application's registry or .INI file.BOOL WriteProfileInt(
       LPCTSTR lpszSection,
       LPCTSTR lpszEntry,
       int nValue 
    );
    Parameters
    lpszSection 
    Points to a null-terminated string that specifies the section containing the entry. If the section does not exist, it is created. The name of the section is case independent; the string may be any combination of uppercase and lowercase letters. 
    lpszEntry 
    Points to a null-terminated string that contains the entry into which the value is to be written. If the entry does not exist in the specified section, it is created. 
    nValue 
    Contains the value to be written. 
    Return Value
    Nonzero if successful; otherwise 0.Res
    The entries are stored as follows: In Windows NT, the value is stored to a registry key. 
    In Windows 3.x, the value is stored in the WIN.INI file. 
    In Windows 95/98, the value is stored in a cached version of WIN.INI. 
    Example
    This example uses CWinApp* pApp = AfxGetApp(); to get at the CWinApp class illustrating a way that WriteProfileString, WriteProfileInt, GetProfileString, and GetProfileInt can be used from any function in an MFC application.   CString strSection       = "My Section";
       CString strStringItem    = "My String Item";
       CString strIntItem       = "My Int Item";   CWinApp* pApp = AfxGetApp();   pApp->WriteProfileString(strSection, strStringItem, "test");   CString strValue;
       strValue = pApp->GetProfileString(strSection, strStringItem);
       ASSERT(strValue == "test");   pApp->WriteProfileInt(strSection, strIntItem, 1234);
       int nValue;
       nValue = pApp->GetProfileInt(strSection, strIntItem, 0);
       ASSERT(nValue == 1234);
       
    =============================
    CWinApp::WriteProfileString()
    =============================   
       Call this member function to write the specified string into the specified section of the application's registry or .INI file.BOOL WriteProfileString(
       LPCTSTR lpszSection,
       LPCTSTR lpszEntry,
       LPCTSTR lpszValue 
    );
    Parameters
    lpszSection 
    Points to a null-terminated string that specifies the section containing the entry. If the section does not exist, it is created. The name of the section is case independent; the string may be any combination of uppercase and lowercase letters. 
    lpszEntry 
    Points to a null-terminated string that contains the entry into which the value is to be written. If the entry does not exist in the specified section, it is created. 
    lpszValue 
    Points to the string to be written. If this parameter is NULL, the entry specified by the lpszEntry parameter is deleted. 
    Return Value
    Nonzero if successful; otherwise 0.Res
    The entries are stored as follows: In Windows NT, the value is stored to a registry key. 
    In Windows 3.x, the value is stored in the WIN.INI file. 
    In Windows 95/98, the value is stored in a cached version of WIN.INI. 
    Example
       CString strSection       = "My Section";
       CString strStringItem    = "My String Item";
       CString strIntItem       = "My Int Item";   CWinApp* pApp = AfxGetApp();   pApp->WriteProfileString(strSection, strStringItem, "test");   CString strValue;
       strValue = pApp->GetProfileString(strSection, strStringItem);
       ASSERT(strValue == "test");   pApp->WriteProfileInt(strSection, strIntItem, 1234);
       int nValue;
       nValue = pApp->GetProfileInt(strSection, strIntItem, 0);
       ASSERT(nValue == 1234);
       
       四、其它相关操作
       AddToRecentFileList Adds a filename to the most recently used (MRU) file list. 
       DelRegTree Deletes a specified key and all its subkeys.