使用注册表或者ini文件来保存和读取程序的配置信息

咱们在编写程序的过程当中,要保存一些用户的设置信息。怎么才能方便的保存和读取呢?ide

通常咱们保存信息由两个地方:1。注册表  函数

                                           2。ni文件it

其实在vc中他们的实现都是同样哦。io

  在应用程序类下的      * App::InitInstance()函数里加入下面的程序语句; class

#ifdef  使用ini文件保存test

char czProgName[MAX_PATH];file

CString czN;二进制

int  iNiValue=0; 程序

//GetModuleFileName()获得程序的路径及名称di

iNiValue= GetModuleFileName(0,czProgName,MAX_PATH);

czProgName[strlen(czProgName)-3] = '\0'; 

strcat(czProgName,"ini"); //咱们的ini文件名是跟程序同名的,只是后缀不一样

free((void*) m_pszProfileName);

m_pszProfileName =_tcsdup(_T(czProgName));

#else //使用注册表文件来保存

free((void*)m_pszRegistryKey);

m_pszRegistryKey = “My test App”;

#endif

加入上面的语句之后,咱们就能够在须要的地方直接用:

固然在使用的时候要定义:  

CWinApp* theApp = NULL;

theApp = AfxGetApp();

theApp ->WriteProfileString( , ,)函数写入ini文件.

在须要读写的地方直接用  :

UINT GetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault);

BOOL WriteProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue);

固然在读取整数的时候,咱们能够分别用

CString GetProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,
            LPCTSTR lpszDefault = NULL);

BOOL WriteProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,
            LPCTSTR lpszValue);

读取二进制数值时候用以下:

BOOL GetProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry,
            LPBYTE* ppData, UINT* pBytes);

BOOL WriteProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry,
            LPBYTE pData, UINT nBytes);

具体请参考MFC下面的APPUI3.cpp

相关文章
相关标签/搜索