The INI file is an extension of the file named "ini". In the Windows system, INI files are many, the most important thing is "System.ini", "System32.ini" and "Win.ini". This file mainly stores the selection of users and various parameters of the system. Users can change many configurations of the application and system by modifying the INI file. However, since the exit of Windows 95, the concept of the registry is introduced in the Windows system. The status of the Ini file will be continuously declining in the status of the Windows system, which is because the unique advantages of the registry makes the applications and systems to initialize many parameters and initialization. The information is placed in the registry. But in some occasions, the INI file also has its irreplaceable position. This article discusses how C # is how to read and write in INI.
INI file structure
The INI file is a text file arranged in a feature manner. Each INI file is very similar, consisting of several paragraphs, under each heading header, is a keyword that starts with a single word (keyword) and a equal sign, the right number is key. Value. The general form is as follows:
[Section1]
Keyword1 = Valuel
Keyword2 = Value2
......
[Section2]
Keyword3 = Value3
Keyword4 = Value4
Program design and operation environment described in this article: ● Microsoft Window 2000 Advanced Server Edition ● .NET Framework SDK official version
C # and Win32 API functions
C # is not like C , has its own class library. C # Used by the class library is a copk, which is available for all .NET programs --.NET Framework SDK. Although the contents of the .NET Framework SDK is very large, the function is also very powerful, but it is not enough, at least it does not provide the related classes needed directly to operate the INI file. In this article, the C # Operation INI file is used by the Windows system comes with Win32 API functions --WritePrivateProfileString () and getPrivateProfileString () functions. These two functions are located in the "kernel32.dll" file. We know that the class libraries used in C # are managed code files, while the files in the Win32 API function are unmanaged code files. This results in a function in which these non-hosting code files cannot be used directly in C #. It is preferred to maintain the next compatibility in order to maintain the next compatibility, and the interoperability is proposed, and the call to the WIN32's API function can be implemented by interoperability. Interoperability not only applies to Win32 API functions, but also use to access managed COM objects. Interoperability of the API function for WIN32 in C # is implemented by the "DLLIMPORT" feature class in the namespace "System.Runtime.InterOpServices". Its main role is to indicate that this property is implemented as an output of the non-host DLL. The following code is in the C # utilizing the "DLLIMPORT" feature class in the namespace "system.Runtime.InterOpServices", "DLLIMPORT" feature classes, the above two Win32 API functions: C # declares the write operation function of the INI file WritePrivateProfileString ():
[DLLIMPORT ("kernel32")] private static extern long writeprivateprofilestring (String
Section,
String Key, String Val, String FilePath;
Parameter Description: Section: Ini files in the paragraph; key: INI file; values in the keyword in the INI file; FilePath: INI file full path and name. C # declares the read operation function of the INI file GetPrivateProfileString ():
[DLLIMPORT ("kernel32")]]
Private static extern int GETPRIVATEPROFILESTRING (String Section,
String Key, String Def, StringBuilder RetVal,
INT size, String filepath;
Parameter Description: Section: The paragraph name in the INI file; key: INI file; DEF: The default value when it is not read; RetVal: Read the value; size: Numerical size; filepath: ini file The full path and name.
Key to read and write ini files in C #
C # writes the INI file: Write an ini file, which is implemented by the "Click" event of component Button2. At this point here, it should be noted that when writing WritePrivateProfileString () writes the INI file, if this INI information will be overwritten if there is the same paragraph name and keyword to be written in the INI file. Below is the code list corresponding to the "Click" event of Button2 components:
Private void button2_click (Object Sender, System.Eventargs E)
{
String filename = textbox1.text;
String sext.com2.text;
String key = textBox3.text;
String KeyValue = TextBox4.Text;
WritePrivateProfileString (Section, Key, KeyValue, FileName);
Messagebox.show ("Successful Writing Ini File!", "Information");
}
C # Reads the INI file: Correctly read INI must meet three premise: the full path, paragraph name and keyword name of the INI file. Otherwise it will not be read correctly. You can set the default value after the unsuccessful value, in the following program, for an intuitive setting is "Unable to read the corresponding value!" String, read the INI file is "Click" event through the Button3 component. To achieve, the following is the corresponding code list:
Private void button3_click (Object Sender, System.Eventargs E)
{
StringBuilder Temp = New StringBuilder (255);
String filename = textbox1.text; string section = textbox2.text;
String key = textBox3.text;
INT i = getPrivateProfileString (Section, Key, "Unable to read the corresponding value!",
TEMP, 255, FileName;
/ / Display the value of the read
TextBox4.text = TEMP.TOSTRING ();
}
C # Operate the intact source code of the INI file (ini.cs) and run interface
Through these introductions above, we don't even have to get a complete code list (INI.cs) with C # operation INI files, as follows:
Using system;
Using system.drawing;
Using system.collections;
Using system.componentmodel;
Using system.windows.forms;
Using system.data;
Using system.Runtime.InteropServices;
Using system.text;
Namespace c_ operation ini file __ write operation
{
Public Class Form1: System.Windows.Forms.form
{
Private system.windows.Forms.Button button1;
Private system.windows.Forms.TextBox textBox1;
Private system.windows.Forms.Button Button2;
Private system.windows.Forms.TextBox textBox2;
Private system.windows.Forms.TextBox textBox3;
Private system.windows.Forms.TextBox textBox4;
Private system.windows.Forms.Label label1;
Private system.windows.Forms.Label label2;
Private system.windows.Forms.Label label3;
Private system.windows.Forms.Button Button3;
Private system.windows.forms.openfiledialog openfiledialog1;
Private system.componentmodel.Container Components = NULL;
Public Form1 ()
{
InitializationComponent ();
}
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
[DLLIMPORT ("kernel32")]]
Private static extern long writPrivateProfileString (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;
Private vidinitiRizeComponent ()
{
This.button1 = new system.windows.Forms.Button ();
This.TextBox1 = new system.windows.Forms.TextBox ();
This.Button2 = new system.windows.Forms.Button ();
This.TextBox2 = new system.windows.Forms.TextBox ();
This.TextBox3 = new system.windows.Forms.TextBox ();
This.TextBox4 = new system.windows.Forms.TextBox ();
THIS.LABEL1 = New System.windows.Forms.label ();
THIS.LABEL2 = New System.windows.Forms.label ();
This.label3 = new system.windows.Forms.label ();
This.Button3 = new system.windows.Forms.Button ();
this.openfiledialog1 = new
System.windows.Forms.openFileDialog ();
THIS.SUSPENDLAYOUT ();
This.button1.flatstyle = system.windows.Forms.Flatstyle.FLAT;
This.button1.location = new system.drawing.point (238, 20);
This.button1.name = "button1";
This.button1.size = new system.drawing.size (100, 32);
this.button1.tabindex = 0;
This.button1.text = "Select Ini File";
This.Button1.click = new system.eventhandler (this.button1_click);
This.TextBox1.Location = New System.drawing.point (58, 22);
THIS.TEXTBOX1.NAME = "TextBox1";
THIS.TEXTBOX1.SIZE = New System.drawing.size (162, 21);
this.TextBox1.tabindex = 1;
THIS.TEXTBOX1.TEXT = "";
This.button2.flatstyle = system.windows.Forms.Flatstyle.FLAT;
This.Button2.Location = new system.drawing.point (86, 168);
This.Button2.name = "button2";
This.button2.size = new system.drawing.size (98, 30); this.button2.tabindex = 3;
This.button2.text = "Write ini file";
This.button2.click = new system.eventhandler (this.button2_click);
this.TextBox2.Location = new system.drawing.point (160, 62);
THIS.TEXTBOX2.NAME = "TextBox2";
This.TextBox2.size = new system.drawing.size (176, 21);
THIS.TEXTBOX2.TABINDEX = 5;
THIS.TEXTBOX2.TEXT = "";
This.TextBox3.Location = New System.drawing.point (160, 94);
This.TextBox3.name = "textbox3";
THIS.TEXTBOX3.SIZE = New System.drawing.size (176, 21);
This.TextBox3.tabindex = 6;
THIS.TEXTBOX3.TEXT = ""
this.TextBox4.Location = new system.drawing.point (160, 128);
This.TextBox4.name = "textbox4";
THIS.TEXTBOX4.SIZE = New System.drawing.size (176, 21);
this.TextBox4.tabindex = 7;
THIS.TEXTBOX4.TEXT = ""
THIS.Label1.Location = new system.drawing.point (56, 62);
THIS.LABEL1.NAME = "label1";
THIS.Label1.tabindex = 8;
THIS.Label1.Text = "Paragraph Name:";
This.Label2.Location = new system.drawing.point (66, 96);
THIS.LABEL2.NAME = "label2";
THIS.LABEL2.TABINDEX = 9;
THIS.LABEL2.TEXT = "Keywords:";
This.Label3.Location = new system.drawing.point (42, 130);
THIS.LABEL3.NAME = "label3";
THIS.Label3.tabindex = 10;
THIS.Label3.Text = "Keyword Numerical:";
This.button3.flatstyle = system.windows.Forms.Flatstyle.FLAT;
This.Button3.Location = new system.drawing.point (208, 168);
This.button3.name = "button3";
This.button3.size = new system.drawing.size (98, 30); this.button3.tabindex = 11;
This.Button3.Text = "Read INI Values";
This.Button3.click = new system.eventhandler (this.button3_click);
This.openfileDialog1.filter = "INI file | * .ini";
THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);
THIS.CLIENTSIZE = New System.drawing.size (366, 217);
this.controls.addrange (new system.windows.forms.control] {
this.button3,
This.TextBox4,
this.TextBox3,
THIS.TEXTBOX2,
this.button2,
this.TextBox1,
this.button1,
THIS.Label3,
THIS.LABEL2,
THIS.LABEL1});
THIS.MAXIMIZEBOX = FALSE;
THIS.NAME = "Form1";
This.Text = "C # Operate INI File - Write";
This.ResumeLayout (false);
}
[Stathread]
Static void main ()
{
Application.run (New Form1 ());
}
Private void Button1_Click (Object Sender, System.Eventargs E)
{
OpenFileDialog1.showdialog ();
TextBox1.text = OpenFileDialog1.FileName;
}
// Write into INI file
Private void button2_click (Object Sender, System.Eventargs E)
{
String filename = textbox1.text;
String sext.com2.text;
String key = textBox3.text;
String KeyValue = TextBox4.Text;
WritePrivateProfileString (Section, Key, KeyValue, FileName);
Messagebox.show ("Successful Writing Ini File!", "Information");
}
/ / Read the value of keywords in the specific paragraph of the specified INI file
Private void button3_click (Object Sender, System.Eventargs E)
{
StringBuilder Temp = New StringBuilder (255);
String filename = textbox1.text;
String sext.com2.text;
String key = textBox3.text;
INT i = getPrivateProfileString (Section, Key,
"Can't read the corresponding value!", EMP, 255, filename); // Display the value of the read
TextBox4.text = TEMP.TOSTRING ();
}
}
}
The picture below is the bundled run interface after INI.CS:
Figure 1: C # Operate the operation interface of the INI file
to sum up
Through these introductions above, you can see the process of c # operation INI files, in fact, is the process of C # calling the Win32 API function. Master how to declare the API function of Win32 in C #, then operate INI is very simple.