C # Summary of INI operations

Source: Internet
Author: User

An INI file is a text file with a specific structure. It consists of three parts:

[Section1]key 1 = value2key 1 = value2……[Section2]key 1 = value1key 2 = value2……

A file consists of several sections. Each section is divided into several keys and values ). The Win32 API functions getprivateprofilestring () and writeprivateprofilestring () in Windows implement read/write operations on inifiles respectively. They are located in kernel32.dll.
Unfortunately, the public class libraries under the. NET Framework used by C # do not provide classes for direct INI File Operations. Therefore, the only ideal method is to call API functions.
Then, the class libraries in the. NET Framework are based on managed code, while API functions are based on unmanaged code. (The Code executed under the control of the Runtime Library is called managed code. Instead, code running outside the Runtime Library is called unmanaged code .) How to implement operations between managed code and non-managed code ?. . NET Framework. runtime. the interopservices namespace provides a variety of members that support the com InterOP and platform call service. One of the most important attributes, dllimportattribute, can be used to define the platform call methods used to access unmanaged APIs, it provides the information necessary to call a function exported from an unmanaged DLL. The following describes how to implement the interoperability between C # and API functions.

Using system; using system. runtime. interopservices; using system. text; namespace function {// <summary> // operation class of the INI file /// </Summary> public class ini {Public String path; Public void INIFILE (string path) {This. path = path;} [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 defval, stringbuilder retval, int size, string filepath); [dllimport ("Kernel32")] private Static extern int getprivateprofilestring (string section, string key, string defval, byte [] retval, int size, string filepath ); /// <summary> /// write the INI file /// </Summary> /// <Param name = "section"> section </param> /// <Param name = "key"> key </param> // <Param name = "ivalue"> value </param> Public void iniwritevalue (string section, string key, string ivalue) {writeprivateprofilestring (section, key, ivalue, this. path );} /// <summary> /// read the INI file /// </Summary> /// <Param name = "section"> section </param> /// <Param name = "key"> key </param> // the key value returned by <returns> </returns> Public String inireadvalue (string section, string key) {stringbuilder temp = new stringbuilder (255); int I = getprivateprofilestring (section, key, "", temp, 255, this. path); return temp. tostring () ;}/// <summary> /// read the INI file /// </Summary> /// <Param name = "section"> segment, format: [] </param> /// <Param name = "key"> key </param> /// <returns> return the section group or key-value group of the byte type </returns> Public byte [] inireadvalues (string section, string key) {byte [] temp = new byte [255]; int I = getprivateprofilestring (section, key, "", temp, 255, this. path); return temp ;}}}

The overload of the function getprivateprofilestring exported by DLL is described as follows:

[Dllimport ("Kernel32")] Private Static extern int getprivateprofilestring (string section, string key, string defval, byte [] retval, int size, string filepath); Section: the paragraph name to be read key: the Key to be read defval: the default value of retval when an exception is read: this parameter type is not string, instead, byte [] is used to return the section group or key-value group of the byte type. Size: The size allowed by the value. filepath: the complete path and file name of the INI file.

The following describes how to instantiate the INIFILE class:
// Path is the physical path of the INI File
INIFILE ini = new INIFILE (PATH );
// Read the names of all paragraphs in the INI File
Byte [] allsection = ini. inireadvalues (null, null );
 
Use the following method to convert byte [] type to string [] array type
String [] sectionlist;
Asciiencoding ASCII = new asciiencoding ();
// Obtain all keys and byte [] types in the custom settings Section
Sectionbyte = ini. inireadvalues ("personal", null );
// Encode the string type of all keys
Sections = ASCII. getstring (sectionbyte );
// Obtain the array of keys
Sectionlist = sections. Split (New char [1] {'\ 0 '});
 
// Read all the key names of the personal section of the INI file and return the byte [] type.
Byte [] sectionbyte = ini. inireadvalues ("personal", null );
 
// Read the model key value of the evideo section of the INI File
Model = ini. inireadvalue ("evideo", "model ");
 
// Write the value eth0 to the device key of the evideo section of the INI File
INI. iniwritevalue ("evideo", "device", "eth0 ");
That is:
[Evideo]
Device = eth0
 
// Delete all keys in the personal section of the INI File
INI. iniwritevalue ("personal", null, null );
 
// Delete all paragraphs in the INI File
INI. iniwritevalue (null, null, null );

In this way, C # implements all the operations on the INI file, including section, key, and key value.

Related Article

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.