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. The structure INI file of the INI file is a text file that is arranged in a characteristic 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. Its general form is as follows: [Section1] Keyword1 = Valuel Keyword2 = Value2 ... [section2] keyword3 = value3 keyword4 = value4 program design and operating environment introduced in this article: ● Microsoft Window 2000 Advanced Server Edition ● .NET Framework SDK official version C # And Win32 API Functions C # are not like C , have their own class libraries. 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 the "Dllimport" feature class in C # using the namespace "DLLIMPORT" feature classes in the name of the two WIN32 API functions: C # declares the write operation function of the INI file WritePrivateProfileString (): [DLLIMPORT ("kernel32 ")] Private static extern long writPrivateProfileString (String section, string key, string val, string filepath); parameter description: section: The paragraph in the INI file; key: Ini file; val: ini file keyword Numerical; FilePath: The complete path and name of the INI file. Read function GetPrivateProfileString C # stated INI file (): [DllImport ( "kernel32")] private static extern int GetPrivateProfileString (string section, string key, string def, StringBuilder retVal, int size, string filePath); Parameters: section : The paragraph name in the INI file; key: in the INI file; DEF: The default value when you cannot read; RetVal: Read the value; size: The size of the value; FilePath: INI file full path and name . The key steps and solutions to the INI file in C # are written to the INI file: written on the 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. The following is a list of the code "Click" event button2 component corresponding to: private void button2_Click (object sender, System.EventArgs e) {string FileName = textBox1.Text; string section = textBox2.Text; string key = textBox3.Text; string keyValue = TextBox4.text; WritePrivateProfileString (Section, Key, KeyValue, FileName); MessageBox.show ("Successful Write 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. achieved, the following is a list of corresponding codes: 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, "You cannot read the corresponding value!", TEMP, 255, filename); // Display the read value textBox4.text = Temp.toString ();} C # Operating the complete source code of the INI file (ini.cs) and the run interface, through these descriptions, we don't even get the full code list (INI.CS) with C # operation INI files, as follows: use: use: use 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_ write operation INI file __ {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; priv ate 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 () {InitializeComponent ();} protected override void Dispose (bool disposing) {if (Disposing) {if (Components! =
null) {components.Dispose ();}} base.Dispose (disposing);} [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); private void InitializeComponent () {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);
THISTBOX1.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.text Box4.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 = "Paramid 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 value:"; 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 value"; 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 # operation INI file - write operation"; THIS .ResumeLayout (false);} [stathread] static void main () {Application.run (new form1 ());} private void but ton1_Click (object sender, System.EventArgs e) {openFileDialog1.ShowDialog (); textBox1.Text = openFileDialog1.FileName;} // write INI file private void button2_Click (object sender, System.EventArgs e) {string FileName = textBox1. Text; string section = textBox2.Text; string key = textBox3.Text; string keyValue = textBox4.Text; WritePrivateProfileString (section, key, keyValue, FileName); MessageBox.Show ( "INI file successfully written! "," Information ");} // read the value of the keyword in the specific paragraph of the specified INI file Private Void Button3_Click (Object Sender, System.EventArgs E) {StringBuilder Temp =