1. 程式人生 > >MFC學習筆記——讀寫配置檔案(.ini)和登錄檔

MFC學習筆記——讀寫配置檔案(.ini)和登錄檔

(一)配置檔案(.ini)

配置檔案中經常用到ini檔案,在VC中其函式分別為:

//寫入.ini檔案:
bool WritePrivateProfileString(LPCTSTR lpAppName,LPCTSTR lpKeyName,LPCTSTR lpString,LPCTSTR lpFileName);

各引數含義如下:
LPCTSTR lpAppName ——– INI檔案中的一個欄位名
LPCTSTR lpKeyName ——– lpAppName 下的一個鍵名,也就是裡面具體的變數名
LPCTSTR lpString ——— 是鍵值,也就是變數的值, 必須為LPCTSTR或CString型別
LPCTSTR lpFileName ——– 完整的INI檔案路徑名

//讀取.ini檔案:
DWORD GetPrivateProfileString(LPCTSTR lpAppName,LPCTSTR lpKeyName,LPCTSTR lpDefaut,LPSTR lpReturnedString,DWORD nSize,LPCTSTR lpFileName);

各引數含義如下:
LPCTSTR lpAppName ——– INI檔案中的一個欄位名
LPCTSTR lpKeyName ——– lpAppName 下的一個鍵名,也就是裡面具體的變數名
LPCTSTR lpDefaut ——— 如果沒有其前兩個引數值,則將此值賦給變數
LPSTR lpReturnedString ——– 接收INI檔案中的值的CString物件,即接收緩衝區
DWORD nSize —— 接收緩衝區的大小
LPCTSTR lpFileName ——– 完整的INI檔案路徑名

//讀取整形值:
UINT GetPrivateProfileInt(LPCTSTR lpAppName,LPCTSTR lpKeyName,INT nDefault,LPCTSTR lpFileName);

例子:
寫入:

CString StrName,Strtemp;
int nAge;
StrName = "jacky";
nAge = 13;
WritePrivateProfileString("Student","Name",StrName,"c:\\setting.ini");

結果:(INI檔案中顯示如下:)

[Student]

Name=jacky

讀取:

CString SName;
GetPrivateProfileString("Student","Name","DefaultName",SName.GetBuffer(MAX_LENGTH),MAX_LENGTH,"c:\\setting.ini");

結果:

SName = “jacky”;

這裡需要注意點就是用完GetBuffer函式後一定要釋放(用SName.ReleaseBuffer()函式),不然後面再用到SName的其他子函式就會失靈。

讀整數比較簡單,如下:

int Result = GetPrivateProfileInt("Student","nAge",0,"c:\\setting.ini")

返回值即為所讀取的結果!

在GetPrivateProfileString最後一個引數是配置檔案路徑的引數,此路徑只能是絕對路徑,不能是相對路徑,但現在我需要是我的exe檔案能和我的配置檔案在一起。因此我使用了GetCurrentDirectory函式

DWORD GetCurrentDirectory(
    DWORDnBufferLength,// size of directory buffer 緩衝區的長度
    LPTSTRlpBuffer// directory buffer 指定一個預定義字串,用於裝載當前目錄
    );

GetCurrentDirectory只是返回作業系統的當前目錄

原始碼如下:

CString server_ip;
 CString des="";
 ::GetCurrentDirectory(MAX_PATHLENGTH,des.GetBuffer(MAX_PATHLENGTH));
 des.ReleaseBuffer();
 des+="\\config.ini";
GetPrivateProfileString("PhoneDemo","Server_IP","",server_ip.GetBufferSetLength(15),15,des);
 server_ip.ReleaseBuffer();

注意:在這裡使用CString變數時,在使用完GetBuffer後,緊接著一定要使用ReleaseBuffer()函式,才可以進行其他的諸如字串+操作。

(二)登錄檔

(1)MFC的CWinApp類提供了訪問登錄檔的函式

void SetRegistryKey( LPCTSTR lpszRegistryKey );
這個函式用來設定the App.m_pszRegistryKey的值,呼叫這個函式可以讓應用程式的引數資訊儲存在登錄檔中,而不是INI檔案中。

用WriteProfileString和WriteProfileInt來對登錄檔進行了寫入操作
用GetProfileInt和GetProfileString對登錄檔進行了讀取操作

注意:這幾個函式只能用於對登錄檔的操作,這也是和WritePrivateProfileString、GetPrivateProfileString的區別,後者是對INI檔案的讀寫

(2)具體操作如下:

// 以下程式碼放到InitInstance中

// 首先清除登錄檔鍵值變數所分配的記憶體空間
free((void*)m_pszRegistryKey); 
// 再清除Ini變數
free((void*)m_pszProfileName); 

// 改變Ini檔名.  
CString filepathtemp =""; 
char filepath[MAX_PATH]={'0'}; 
::GetCurrentDirectory(MAX_PATH,filepath); 
filepathtemp=CString(filepath); 
if(filepathtemp.Right(1)!="\\") 
{ 
    filepathtemp+="\\"; 
} 
CString profilepath = filepathtemp+"canshu.ini"; 
m_pszProfileName=_tcsdup("profilepath ");  
theApp.WriteProfileString( "學生","姓名","張三");  

進行完以上設定後我就可以直接使用GetProfileString 、WriteProfileString等函式進行操作了。

CWinApp成員函式
CWinApp::GetProfileString
CString GetProfileString( LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault = NULL );
1、返回值
返回值是應用程式的.INI檔案中的字串,如果找不到該字串,則為lpszDefault。框架支援的字串最大長度為_MAX_PATH。如果lpszDefault為NULL,則返回值是一個空字串。
2、引數: lpszSection 指向一個以null結尾的字串,指定了包含入口的部分。
3、說明
呼叫這個函式以獲得與應用程式的登錄檔或.INI檔案中指定部分的入口相關的字串。
這些入口按照如下方式儲存:
·Windows NT 該值儲存在登錄檔中
·Windows 3.X 該值儲存在WIN.INI檔案中
·Windows 95 該值儲存在WIN.INI的緩衝版本中
4、示例

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

(3)登錄檔存放的位置:
開始-執行->輸入regedit->HEY-CURRENT-USER->Software下,找即可。