Non-registry saving class (INI file operation class)

zhaozj2021-02-08  196

The current procedures generally record some of their own running information, what is the place to save? Microsoft's recommendations are saved in the registry so that it is not easy to lose. But recently I know some ordinary computer users, users do not want the program to write non-systematic information to the registry, avoiding the registry increasing, now usually using computer, general registration forms are more than 30m The startup speed of the computer slows down. So I have written a class that operates the INI file with the previous function of Windows, saving the running information of the program in the INI file in the current directory.

This class can directly read the four data of String, Int, DWORD, DOULE, easy to operate, and the specific code is as follows:

#define max_length 256

Class cinifile {public: cinifile (); virtual ~ cinifile ();

Void setiniFileName (cstring filename) {inIfilename = filename;

Cstring getinifilename () {return inifilename;}

CString GetString (CString AppName, CString KeyName, CString Default = ""); int GetInt (CString AppName, CString KeyName, int Default = 0); double GetDouble (CString AppName, CString KeyName, double Default = 0); unsigned long GetDWORD ( CSTRING AppName, CString Keyname, Unsigned long default = 0);

BOOL SetString (CString AppName, CString KeyName, CString Data); BOOL SetInt (CString AppName, CString KeyName, int Data); BOOL SetDouble (CString AppName, CString KeyName, double Data); BOOL SetDWORD (CString AppName, CString KeyName, unsigned long DATA);

PRIVATE: CSTRING INIFILENAME;

Cinifile :: cinifile () {char szappname [max_path]; int Len;

:: getModuleFileName (AFXGetInstanceHandle (), Szappname, SizeOf (Szappname)); LEN = Strlen (Szappname); for (int i = len; i> 0; I -) {IF (szappname [i] == '.'. ' ) {Szappname [i 1] = '/ 0'; Break;}} strcat (szappname, "ini"); inIfilename = szappname;}

CINIFILE :: ~ cinifile () {

}

CString CIniFile :: GetString (CString AppName, CString KeyName, CString Default) {TCHAR buf [MAX_LENGTH]; :: GetPrivateProfileString (AppName, KeyName, Default, buf, sizeof (buf), IniFileName); return buf;}

double CIniFile :: GetDouble (CString AppName, CString KeyName, double Default) {TCHAR buf [MAX_LENGTH]; CString temp; temp.Format ( "% f", Default); :: GetPrivateProfileString (AppName, KeyName, temp, buf, sizeof (buf), IniFileName); return atof (buf);} int CIniFile :: GetInt (CString AppName, CString KeyName, int Default) {return :: GetPrivateProfileInt (AppName, KeyName, Default, IniFileName);}

unsigned long CIniFile :: GetDWORD (CString AppName, CString KeyName, unsigned long Default) {TCHAR buf [MAX_LENGTH]; CString temp; temp.Format ( "% u", Default); :: GetPrivateProfileString (AppName, KeyName, temp, buf , Sizeof (BUF), INIFILENAME; RETURN ATOL (BUF);

Bool Cinifile :: SetString (CString Appname, CString Keyname, CString Data) {Return :: WritePrivateProfileString (Appname, Keyname, Data, IniFileName);

Bool Cinifile :: Setint (CString Appname, CString Keyname, INT DATA) {CString Temp; Temp.Format ("% D", DATA); RETURN :: WritePrivateProfileString (Appname, Keyname, Temp, Inifilename);}

BOOL CIniFile :: SetDouble (CString AppName, CString KeyName, double Data) {CString temp; temp.Format ( "% f", Data); return :: WritePrivateProfileString (AppName, KeyName, temp, IniFileName);}

BOOL CIniFile :: SetDWORD (CString AppName, CString KeyName, unsigned long Data) {CString temp; temp.Format ( "% u", Data); return :: WritePrivateProfileString (AppName, KeyName, temp, IniFileName);}

转载请注明原文地址:https://www.9cbs.com/read-2927.html

New Post(0)