INI file is a file with the extension "INI". In the Windows system, the INI file is a lot, the most important thing is "System.ini", "System32.ini" and "Win.ini". This file mainly holds the selections made by the user and the various parameters of the system. Users can modify the INI file to change many of the application and system configuration. But since Windows 95 's exit, the concept of the Registry has been introduced into Windows, and the INI file has been declining in the Windows system because of the unique advantages of the registry, which allows applications and systems to put many parameters and initialization information into the registry. But in some cases, the INI file also has its irreplaceable status. This article will discuss how C # is read and write to the INI.
The structure of the INI file
An INI file is a text file that is arranged in a way that is characteristic. Each INI file composition is very similar, consists of several paragraphs (section), under each parenthesized heading, is a number of words beginning with a single word (keyword) and an equal sign, the equal sign to the right is the value of the keyword (value). The general form is as follows:
[Section1]
KeyWord1 = Valuel
KeyWord2 = Value2
......
[Section2]
KeyWord3 = Value3
KeyWord4 = Value4
C # and Win32 API functions
C # does not have a class library of its own, like C + +. The class library used by C # is a common class library ——. NET Framework SDK that is provided by the. NET Framework for all. NET program development. Although the. Net FrameWork SDK is very large and powerful, it is not exhaustive, at least it does not provide the related classes needed to directly manipulate the INI file. In this article, the C # operations INI file uses the API functions--writeprivateprofilestring () and getprivateprofilestring () functions that are Win32 by the Windows system. These two functions are located in the "Kernel32.dll" file.
We know that the class libraries used in C # are managed code (Managed code) files, and Win32 API functions are files that are unmanaged (unmanaged code) files. This leads to the impossibility of directly using functions in C # in these unmanaged code files. Fortunately. NET Framework in order to maintain the compatibility of the next, also in order to make full use of the previous resources, the interoperability, through interoperability can be implemented on the WIN32 API function calls. interoperation applies not only to Win32 API functions, but also to managed COM objects. The interoperability of WIN32 API functions in C # is achieved through the "DllImport" feature class in the namespace "System.Runtime.InteropServices". Its primary role is to indicate that this attributed method is implemented as an output of an unmanaged DLL. The following code uses the "DllImport" feature class in the namespace "System.Runtime.InteropServices" in C # to declare the above two WIN32 API functions:
C # declaration INI file write operation function writeprivateprofilestring ():
Parameter description: The paragraph in the Section:ini file, the keywords in the Key:ini file, the number of keywords in the Val:ini file, and the full path and name of the Filepath:ini file.
C # declaration INI file read operation function getprivateprofilestring ():
[DllImport ("kernel32")]
private static extern int getprivateprofilestring (string section,
String key, String Def, StringBuilder RetVal,
int size, string filePath);
Parameter description: The paragraph name in the Section:ini file; key:ini the keyword in the file; def: the default value when it cannot be read; RetVal: reads the value, size: The value, the full path and name of the Filepath:ini file.
The following is a class that reads and writes the INI file
public class Iniclass
{
public string IniPath;
[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);
<summary>
Construction method
</summary>
<param name= "INIPath" > File path </param>
Public Iniclass (String INIPath)
{
IniPath = IniPath;
}
<summary>
Write INI file
</summary>
<param name= "section" > Project name (e.g. [TypeName]) </param>
<param name= "key" > Key </param>
<param name= "Value" > Value </param>
public void Iniwritevalue (string section,string key,string Value)
{
WritePrivateProfileString (Section,key,value,this.inipath);
}
<summary>
Read the INI file
</summary>
<param name= "section" > Project name (e.g. [TypeName]) </param>
<param name= "key" > Key </param>
public string Inireadvalue (String section,string Key)
{
StringBuilder temp = new StringBuilder (500);
int i = getprivateprofilestring (Section,key, "", Temp,500,this.inipath);
Return temp. ToString ();
}
<summary>
Verify that the file exists
</summary>
<returns> Boolean value </returns>
public bool Existinifile ()
{
Return file.exists (IniPath);
}
}
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.