INI file read/write Solution

Source: Internet
Author: User

Chapter 1 Description of the INI File

INI document description can refer to Baidu Encyclopedia: http://baike.baidu.com/link? Url = Wq0o_qdZdByktz_JBNFSBQU1mgE4iViIUE8GmEzG7Td-FVwycsVd-xSSNKgqgvedMkOZEgAFXA6d0YOOhfCVc _

Chapter 2 Interface

Generally, large ERP projects usually have a lot of initial information to be configured in the configuration file. This involves reading and managing configuration files.

This interface uses Object-Oriented Programming habits to abstract configuration information into a class or an object. The configuration information class only inherits the IniAbs interface, and the configuration information class can be read and saved.

Example:

To configure database connection string information, you must store the database server, database name, user name, password, and other information in the configuration file. We only need to build the class DBConfig:

 [IniSection(Description=,Section=    DBConfig : IniAbs<DBConfig>           [IniFieldDescription(Discription = , Key =             DBName { ;              [IniFieldDescription(Discription = , Key =             DBServer { ;              [IniFieldDescription(Discription = , Key =             DBUsername { ;              [IniFieldDescription(Discription = , Key =             DBPassword { ;   }

 

 

Then, we can use the factory method to read the configuration information:

DBConfig dbConfig = IniInstanceFactory.GetSingleInstance<DBConfig>();

 

To save configuration information, you only need to callDbConfig. Save ()MethodTo automatically save the configuration information to the file.

 

Chapter 3 configuration file interface project source code analysis

All configuration file read operations are encapsulated in IFConfiguration. dll.

The IFConfiguration project is mainly divided into three parts:

I. configuration information storage node description

IniSectionAttrbute indicates the configuration information class, which indicates the Section name stored in the INI file.

IniFieldDescriptionAttribute indicates the configuration information field, which stores the corresponding name (parameter name) in the INIFILE)

Ii. ini file operation tool

 

This class is mainly used to package Windows API method calls:

/// Declare the API function for reading and writing INI files [DllImport ("kernel32")] private static extern long WritePrivateProfileString (string section, string key, string val, string filePath ); [DllImport ("kernel32")] private static extern int GetPrivateProfileString (string section, string key, string def, StringBuilder retVal, int size, string filePath );

 

WritePrivateProfileString and GetPrivateProfileString are the methods COM in the kernel32 dynamic link library.

3. Create a factory for the configuration file information base class and Configuration File Information Class Object

 

IniAbs <T> is the basic class of the configuration file information class. The Save method saves the configuration file information Class Object to the INI file.

  (!=  RWini(Application.StartupPath +              ( iniField  .Section, iniField.Key, .IniFieldProperty[iniField].GetValue(, 

 

The IniInstanceFactory class is used to configure the Information Class Object creation factory. The GetInstance method <T> () and GetSingleInstance <T> () obtain the instance of the configuration information class T. The instance will read the INI file to initialize the instance.

Chapter 4 Technical Description

This interface uses the object-oriented idea to save and read configuration information and encapsulate it into an object. This solution has the following three advantages: 1. Easy to read configuration information. You only need to call the IniInstanceFactory method GetInstance to read configuration information. 2. Easy to save configuration information and want to modify configuration information, you only need to operate on the object and then call the Save method to Save it. 3. easy configuration instructions: for a large project, there may be a lot of configuration information, in this way, management, especially the preparation and maintenance of practical configuration instruction documents, is a very headache, because the configuration information descriptions are all practical standard features IniSectionAttrbute, IniFieldDescriptionAttribute descriptions, it is easy to create analysis tools, the help document is automatically generated based on the configuration information code file.

This interface mainly includes technology or C # theory: generics, interfaces, Windows API calls, inheritance, Polymorphism

 

Source code download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.