[DEV] Programming Experience: How to save configuration information

xiaoxiao2021-03-06  44

One of my programming experiences for program configuration information

In every program we have written, we must save some useful information, how to save this information? There are three commonly used methods, one is to use the registry, one is to use the INI file, and it is to use the file. That ways to save configuration information, different people may have different habits, these three methods are the most suitable for you, what is the most convenient? First, use the registry in the BCB with a Tregistry, which provides a convenient registry operation, which has several important properties and methods. As follows: __property hkey rootkey = {read = frootkey, write = setKey, NodeFault}; this property is used to set and get the current root key. By default it is HKEY_CURRENT_USER. This attribute is usually usually used. BOOL __FASTCALL OPENKEY (Const Ansistring Key, Bool Cancreate); This method is used to open a key, and Bool Cancreate means that if this button does not exist, whether the key is created. Returns true if it turns on successfully. Void __fastcall closekey (void); This method closes the currently open button. When you complete the registry, you should call this method to save your changes. BOOL __FASTCALL Keyexists (const Ansistring key); This method determines if a button exists. BOOL __FASTCALL VALUEEXISTS (Const Ansistring Name); This method determines whether there is a specified data item under the current key. Ansistring __fastcall readstring (const anstring name); This method reads a string from the data item specified under the current button. Void __fastcall writeString (const anstring name, const aion value); This method writes a string to the data item specified under the current key. Similar functions include readinteger, Writeinteger, ReadBool, WriteBool, etc., here is not listed, please take a look at the online help, let's take an example of using the registry to save and read program configuration information. To illustrate how to read and save the configuration information of the program with the registry. The following example reads the configuration information from the hkey_local_machine // suffware // myinfo key, and this button is created if the program is the first run. (Use the Tregistry class, you need to contain registry.hpp) #include

void __fastcall ReadConfig () {TRegistry * reg = new TRegistry (); reg-> RootKey = HKEY_LOCAL_MACHINE; if (reg-> OpenKey ( "// Software // MyInfo", true)) {// Open the HKEY_LOCAL_MACHINE // Software / / Myinfo button If there is no existence, create if ("DataPath")) {// determines whether there is a DataPath data item, exist, read sysconfig.dataPath = REG-> ReadString ("DataPath");} ELSE {// If this data item does not exist, the program default path sysconfig.dataPath = defaultpath;}} reg-> closekey (); // Close the open button. DELETE REG;} The following example is to save information to the registry, if this button is not created and saved. void __fastcall SaveConfig () {TRegistry * reg = new TRegistry (); reg-> RootKey = HKEY_LOCAL_MACHINE; if (reg-> OpenKey ( "// Software // MyInfo", true)) {// Open the HKEY_LOCAL_MACHINE // Software / / Myinfo button If there is no existence, create reg-> Writestring ("DataPath", sysconfig.dataPath);} reg-> closekey (); // Close the open key and save the information delete reg;} These two examples are simple, Some other read and write methods are also similar and written in String, and you can try it. Use the registration table to pay attention to the problem.

Using the registry to save configuration information is the method used by most software, the advantage of the registry is that the user can easily achieve and modify these configuration information, and it is possible to have certain security and concealment. If you use a binary method, Users will be difficult to know the content you saved, so that your data is more secure. It is what you have to pay attention to in different versions of Windows, and the structure of the registry is different, and under NT and Win2000, if Sufficient permissions are not enough, some key systems are not allowed to access. Then the user is annoying software to write something in its registry. This is the popular green software. Second, use the INI configuration file

In the BCB, there is a TiniFile class, which provides an operation of the INI file, an universal configuration file format of the INI file, which is also the same as the registry, the following is the content of an INI configuration file, it has one Key LastConfig, there is a data item defaultdit under this button, which is the value of this data item after the data item. [LastConfig] Defaultdir = E: / Wang Le Dong Program / SJGL Third Edition TiniFile class There are several common methods to introduce: __fastcall tinifile (const anstring filename): Inifiles :: tcustomIfile (filename) {} This method creates an ini Object, if the file name specified by FileName does not exist, this file is automatically generated, which can use the full path to specify the location of the INI file. If only the file name has no path, this function will go to the Windows system path to open or create. (Win9x is a Windows directory, NT and 2000 are winnt directories) BOOL __FASTCALL SectionExists (const anstring section); this method determines whether this button is present. Virtual anstring __fastcall readstring (constings); this method reads a string from the INI file, section is the specified key, Ident is the data item under the specified key DEFAULT if this item is not The default value is present. Virtual void __fastcall writString (const on means, const aion "; this method is to write a string in the INI file, and the SENction is the specified key. Ident is the specified data item, Value is to write. data. Like TregiSTRY, TiniFile class also provides reading and writing methods for reading integer, Boolean, binary and other data types. Let's take a look at online help, usage basically. Let's take the INI file above as an example to illustrate how you have an INI file to save configuration information and how to read data from the INI file. The following example is how to read configurations from an INI file, this profile is saved in the application's directory, if not configured, the path is located is the default value.

#include

void __fastcall ReadConfig () {// read the configuration, TIniFile * regKey; AnsiString ExePath = ExtractFileName (ParamStr (0)); regKey = new TIniFile (ExePath "TrimTxt.ini"); DefaultDir = regKey-> ReadString ( "LastConfig "," Defaultdir ", execPath; delete regkey;}

The following example shows how to write information to the INI file

void __fastcall SaveConfig () {TIniFile * regKey; AnsiString ExePath = ExtractFileName (ParamStr (0)); regKey = new TIniFile (ExePath "TrimTxt.ini"); regKey-> WriteString ( "LastConfig", "DefaultDir", DefaultDir) It is very convenient to save configuration information using the INI file. It is the biggest advantage that the user can change it manually. It is usually used to save some data not critical. Its disadvantage is that it is not suitable for data complicated data, and files are easily destroyed.

Third, using the file using the file to save the configuration of the program, probably not often used. Because the operation of the file is something that makes a lot of beginners, but through the appropriate method, it is very convenient and flexible with document storage data. There are several ways to use files in BCB. A, use Fopen, FWRITE compare tradition, but it is more flexible. B, use stream, such as FileStream and MemoryStream, the advantage is relatively convenient. Save data using files I often use by defining a structure, operation, as follows

Struct {char username [10]; // User name char password [16]; // User password INT level [24]; // User rights} UserInfo;

This structure is used to save some information from the program user, (I am not necessarily reasonable, just for an example) If you save it with the registry and INI files, it is very troublesome, but if you use the file very simple. Below I use A, B in two ways to save and read. The following two examples read and write the configuration file with a traditional file reading method.

#include

BOOL __FASTCALL READCONFIG () {// Read Configuration File File * Ptr; Ansistring FileName = ExtractFileName (paramstr (0)) "UserInfo.dat"; PTR = fopen (filename.c_str (), "RB"); IF Ptr == null) {showMessage ("file open failed!"); returnaf (PTR, 0L, seek_ek); // Position to file tail IF (Ftell (PTR)! = sizeof (userInfo) {/ / Judge whether the file is destroyed. ShowMessage ("Profile is destroyed!"); Fclose (PTR); Reutrn false;} fseek (PTR, 0L, seek_set); // Position to File Headfread (& UserInfo, Sizeof (UserInfo), 1, PTR); / / Read the content to the structure. Fclose (PTR); Return True;

Void __fastcall saveconfig () {file * ptr; Ansistring filename = extractFileName (paramstr (0)) "userinfo.dat"; ptr = fopen (filename.c_str (), "wb"); if (ptr == null) { ShowMessage ("File Open Failed!"); Return False;} FWRITE (& UserInfo, SizeOf (UserInfo), 1, PTR); // Read the content to the structure. This will complete the reading and writing of a profile (PTR);}. The following is manipulated with stream. BOOL __FASTCALL READCONFIG () {// reads the flow, I use memory flow because of my personal habits, the same use of the file stream. TMemoryStream * readStream = new TMemoryStream (); AnsiString FileName = ExtractFileName (ParamStr (0)) "UserInfo.Dat"; readStream-> LoadFromFile (FileName); if (! ReadStream-> Size = sizeof (UserInfo)) {ShowMessage ( "Profile is destroyed!"); Delete readstream; Reutrn false;} Readstream-> Seek (0, SOFROMBEGINNING); / / Location to File Header ReadStream-> ReadputBuffer (& UserInfo, Sizeof (UserInfo)); // Read file Content to structure delete readstream;

void __fastcall SaveConfig () {TMemoryStream * readStream = new TMemoryStream (); AnsiString FileName = ExtractFileName (ParamStr (0)) "UserInfo.Dat"; readStream-> WriteBuffer (& UserInfo, sizeof (UserInfo)); // write the structure Entering ReadStream-> Seek (0, SOFROMBEGINNING); // Located to head readstream-> savetofile (filename); DELETE READSTREAM;} This completes to read and write data, through the comparison of these two methods, easy to use some. The disadvantage of saving data with documents is some cumbersome, and files are easy to delete, but its advantages can save large, complex data, and cannot be done using the registry and INI files.

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

New Post(0)