1. 程式人生 > >MFC讀取和寫ini檔案

MFC讀取和寫ini檔案

本文內容為從當前執行目錄下的SysCfg.ini檔案中讀寫配置,工程目錄:http://download.csdn.net/detail/dafenqie/9699140

1、首先定義全景變數,用來儲存配置

CString g_strVideoPath;
CString g_strPtzVideoPath;
CString g_strFixedVideoPath;


CString g_strPhotoPath;
CString g_strPtzPhotoPath;
CString g_strFixedPhotoPath;


//定焦視訊IP
 CString g_strFixedVideoIP;
 CString g_strIntvSysIP;
 //變焦視訊IP
 CString g_strPtzVideoIP;
 CString g_strPtzUser;
 CString g_strPtzPassWord;


 DWORD g_dwFixedVideoPort;
 WORD g_wPtzPort;


 unsigned long g_fixedVideoSpace;
unsigned long g_ptzVideoSpace;

2、寫讀配置的函式,在對話方塊介面上增加一個文字框,關聯變數m_strEdit用來顯示讀取出來的配置,增加一個按鈕,在按鈕中呼叫以下函式,顯示配置到文字框中。

void CtestCfgIniDlg::GetSysCfg()
{
//向D盤寫入程式資料庫連線ini檔案資訊,預設設定如下  
//查詢是否存在ini檔案,若不存在,則生成一個新的預設設定的ini檔案,這樣就保證了我們更改後的設定每次都可用  
CFileFind finder;
BOOL ifFind = finder.FindFile(".\\SysCfg.ini");  
if( !ifFind )  
{  
::WritePrivateProfileString("CameraCfg","ip","127.0.0.1",".\\SysCfg.ini");  
::WritePrivateProfileString("CameraCfg","port","9070",".\\SysCfg.ini");  

::WritePrivateProfileString("PtzCameraCfg","ip","192.168.1.238",".\\SysCfg.ini"); 
::WritePrivateProfileString("PtzCameraCfg","port","8000",".\\SysCfg.ini"); 
::WritePrivateProfileString("PtzCameraCfg","user","admin",".\\SysCfg.ini"); 
::WritePrivateProfileString("PtzCameraCfg","password","123456abc",".\\SysCfg.ini"); 


::WritePrivateProfileString("RecordCfg","fixedVideoSpace","200",".\\SysCfg.ini");
::WritePrivateProfileString("RecordCfg","ptzVideoSpace","100",".\\SysCfg.ini");


::WritePrivateProfileString("RecordCfg","videoPath", "E:\\video",".\\SysCfg.ini"); 
::WritePrivateProfileString("RecordCfg","photoPath", "E:\\photo",".\\SysCfg.ini");
}  


    ::GetPrivateProfileString("CameraCfg","ip","127.0.0.1",g_strFixedVideoIP.GetBuffer(MAX_LENGTH),MAX_LENGTH,".\\SysCfg.ini"); 
    g_strFixedVideoIP.ReleaseBuffer();
m_strEdit += g_strFixedVideoIP+"\r\n";


g_dwFixedVideoPort = ::GetPrivateProfileInt("CameraCfg","port",9070,".\\SysCfg.ini");  
CString strTmp;
strTmp.Format("%d", g_dwFixedVideoPort);
m_strEdit += strTmp+"\r\n";

::GetPrivateProfileString("PtzCameraCfg","ip","192.168.1.238", g_strPtzVideoIP.GetBuffer(MAX_LENGTH), MAX_LENGTH,".\\SysCfg.ini"); 
g_strPtzVideoIP.ReleaseBuffer();
m_strEdit += g_strPtzVideoIP+"\r\n";


g_wPtzPort = ::GetPrivateProfileInt("PtzCameraCfg","port",8000,".\\SysCfg.ini");  
strTmp.Format("%d", g_wPtzPort);
m_strEdit += strTmp+"\r\n";


::GetPrivateProfileString("PtzCameraCfg","user","admin", g_strPtzUser.GetBuffer(MAX_LENGTH), MAX_LENGTH,".\\SysCfg.ini");
g_strPtzUser.ReleaseBuffer();
m_strEdit += g_strPtzUser+"\r\n";;
::GetPrivateProfileString("PtzCameraCfg","password","123456abc",g_strPtzPassWord.GetBuffer(MAX_LENGTH), MAX_LENGTH,".\\SysCfg.ini"); 
g_strPtzPassWord.ReleaseBuffer();
m_strEdit += g_strPtzPassWord+"\r\n";




//儲存設定,包括視訊儲存支援的空間(G),抓拍圖片的個數,視訊儲存每個檔案的時間(分鐘),視訊和圖片的儲存路徑
g_fixedVideoSpace = ::GetPrivateProfileInt("RecordCfg", "fixedVideoSpace", 200, ".\\SysCfg.ini");
strTmp.Format("%d", g_fixedVideoSpace);
m_strEdit += strTmp+"\r\n";
g_ptzVideoSpace = ::GetPrivateProfileInt("RecordCfg", "fixedVideoSpace", 100, ".\\SysCfg.ini");
strTmp.Format("%d", g_ptzVideoSpace);
m_strEdit += strTmp+"\r\n";


::GetPrivateProfileString("RecordCfg","videoPath","E:\\video",g_strVideoPath.GetBuffer(MAX_LENGTH), MAX_LENGTH,".\\SysCfg.ini"); 
g_strVideoPath.ReleaseBuffer();
m_strEdit += g_strVideoPath+"\r\n";


::GetPrivateProfileString("RecordCfg","photoPath","E:\\photo",g_strPhotoPath.GetBuffer(MAX_LENGTH), MAX_LENGTH,".\\SysCfg.ini"); 
g_strPhotoPath.ReleaseBuffer();
m_strEdit += g_strPhotoPath+"\r\n";
UpdateData(false);




}