The Ini file is a more common software profile format on the Windows platform, and Windows applications often use it to save some configuration information. It is generally composed of several components containing key-value pairs, each Key-Value holds some software configuration information. For example, the most typical NT series startup configuration file Boot.ini:
[boot loader] Timeout = 30 default = MULTI (0) DISK (0) rdisk (0) Partition (2) / windows [Operating Systems] Multi (0) Disk (0) RDisk (0) Partition (2) / Windows = "Microsoft Windows XP Professional" / FastDetect Multi (0) Disk (0) RDisk (0) Partition (1) / Windows = "Microsoft Windows XP Professional" / FastDetect
In this file, the string in square brackets is the name of the section, and the content between the two square brackets is a section. The section content is some Key-Value pair, each key-value is occupied, such as Timeout = 30 is a pair of Key-Value pairs, Timeout is Key, the corresponding value is 30. The Windows platform provides a set of APIs to easily operate the INI file, such as getPrivateProfileSection (), getPrivateProfileint (), etc..
With the continuous development of the Windows series operating system, the role of the INI file is gradually replaced by confirms, XML format Config files, etc., rarely used for system configuration, but we can still use it in the application. The software profile format recommended in the .NET platform is based on XML-based config files, so there is no special support for the INI file read and written in the .NET Framework, so that we sometimes do not be very convenient when you need to read and write the INI file. . This article will explore how to make the read and write of the INI files easier on the .NET platform. Of course, we can introduce the above API directly, but this article will not use the API, but is completely based on .NET Framework.
Create an INI file read and write class
To handle Ini files on the .NET platform, natural ideas is to create a special Class to be responsible for the reading and writing of the INI file, this class exposes the appropriate interface for external calls. The general INI file size is small, so the simplest approach is to read the entire file in a string variable in the text manner. The class is defined as follows:
Public Class Fileini
{
PRIVATE STRING FileContents = NULL;
Public Fileini (String filename)
{
File.exists (filename))
{
StreamReader R = file.opentext (filename);
FileContents = R.ReadToend ();
R.Close ();
}
}
}
Next, we must provide some ways to operate this string, such as returning all Section Name, acquiring Value corresponding to a specific key. We can use a method such as a string to complete these work, but .NET Framework provides us with a better way, that is, regular expressions.
Regular expression
The so-called regular expression is a language designed to optimize string operations. It uses a set of metachacters to achieve strong string operational capabilities. This component character comes earlier from the extension of the DOS file system? In the DOS file system,? And * are used instead of individual characters and character groups, which can be considered the earliest metadature. Regular expressions are constantly expanding on their basis, forming a set of element character sets, which can express a very complex string. For example, users often need to enter a valid email address when registering online. How do we verify that this Email address is legal after entering a string? Use the following regular expression to easily achieve purposes:
@ "^ ([/ w - /.] ) @ (/ [[[0-9] {1, 3} /. [0-9] {1, 3} /. [0-9] {1, 3} /.) (([/ W -] /.)) ([A-ZA-Z] {2, 4} | [0-9] {1,3}) (/]?) $ "
Regarding this regular ratio expression, don't explain too much, interested friends can refer to relevant regular expressions. This regular expression can not guarantee the authenticity and effectiveness of the Email address entered by the user, but at least the user entered the Email address seems to be legally effective.
Some classes that use regular expressions in .NET Framework, these classes are located under System.Text.RegularExpressions namespace.
Use regular expressions to implement the Fileini class
Now we can use regular expressions to implement the corresponding features of the Fileini class. In order to return the name of all Section in the INI file, we can use a read-only properties SectionNames to return a sections name string array.
Public String [] SectionNames
{
get
{
// USING Regular Expression to get all section.
String regexpattern = @ "/ [(?
Regex r = new regex (regexpattern); // match "[Anywords]"
Matchcollection matches = r.matches (fileContents);
// Writing all section name to a string array.
String [] results = new string [matches.count];
For (int i = 0; i { Results [i] = matches [i] .Result ("$ {sectionname}"); } Return Results; } } In the above code, we use a regular expression: @ "/ [(? (? (? (? (?", A matching of the source string is taken out of all section Name. In order to get the specific Key Value under certain section, we must first get all the content under this section, then remove the Value of a specific key. Public string getsectionstring (String Sectionname) { String regexpattern = @ "(/ [" sectionname @ "/]" @ "(? Regex r = new regex (regexpattern, regexoptions.singlex); IF (R.Ismatch (FileContents)) { Return R.Match (FileContents) .Result ("$ {sectiontring}); } Return string.empty; } GetSectionstring () obtains all the contents of this section based on a particular sectionName. Suppose sectionName is a string boot loader, at this time, the regular expression is @ "(/ [boot loader /] (? Public string getKeystring (String section, String Keyname) { String sectionstring = this.getsectionstring (sectionname); String regexpattern = @ "(" keyname @ "= (? Regex r = new regex (regexpattern); IF (R.Ismatch (FileContents)) { Return R.Match (FileContents) .result ("$ {Value}"); } Return string.empty; } On this basis, more methods such as getKeyint () can be obtained. As for the write method, it is easy to implement using Regex's Replace () method, and there is not much narrative. to sum up This article focuses on the application of regular expressions when reading and writing INI files. The implemented INI file read and write fileini scalability is slightly insufficient, for example, this class can only handle INI files in general format, which is slightly changing in the format, and the regular expressions in this class need to be modified. In short, the regular expression is the powerful tool for processing the string, mastering it It is absolutely good to handle strings more efficiently.