This article mainly for you in detail the C # operation INI configuration file example, with a certain reference value, interested in small partners can refer to
The example of this article for everyone to share the C # operation INI configuration file Examples of specific code for your reference, the specific content is as follows
Source file Address: C # operation INI configuration file example
The Created control:
Source:
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.linq;using system.text;using system.windows.forms;using system.runtime.interopservices;namespace windowsformsapplication3{public partial class Form1:form {public Form1 () {InitializeComponent (); } [DllImport ("Kernel32.dll")] private static extern long WritePrivateProfileString (string section, string key, Stri Ng value, string filepath); [DllImport ("Kernel32.dll")] private static extern int getprivateprofilestring (string section,string key,string def,str Ingbuilder returnvalue,intbuffersize,string filepath); private string IniFilePath; private void Form1_Load (object sender, EventArgs e) {combobox1.text = "male"; for (int i = 1; i <=; i++) {ComboBox2.Items.Add (i.tostring ()); } combobox2.text = "18"; IniFilePath = Application.startuppath + "\\Config.ini"; } private void ButtOn1_click (object sender, EventArgs e) {if ((TextBox1.Text.Trim () = "") && (TextBox2.Text.Trim ()! = "")) {string section = "Information"; try {writeprivateprofilestring (section, "Name", TextBox1.Text.Trim (), IniFilePath); WritePrivateProfileString (section, "Gender", Combobox1.text, IniFilePath); WritePrivateProfileString (section, "Age", Combobox2.text, IniFilePath); WritePrivateProfileString (section, "Region", TextBox2.Text.Trim (), IniFilePath); } catch (Exception ee) {MessageBox.Show (EE. Message); }} else {MessageBox.Show ("name or region cannot be empty! "," Error ", MessageBoxButtons.OK, messageboxicon.warning); }} private void Button2_Click (object sender, EventArgs e) {string outstring; try {GetValue ("information", "Name", out outstring); TextBox1.Text = outstring; GetValue ("Information", "Gender", out outstring); Combobox1.text = outstring; GetValue ("Information", "age", out outstring); Combobox2.text = outstring; GetValue ("Information", "region", out outstring); TextBox2.Text = outstring; } catch (Exception ee) {MessageBox.Show (EE. Message); }} private void GetValue (String section,string key, out string value) {StringBuilder StringBuilder = new StringBuilder (); GetPrivateProfileString (section, Key, "", StringBuilder, 1024x768, IniFilePath); Value = Stringbuilder.tostring (); } private void Button3_Click (object sender, EventArgs e) {TextBox1.Text = ""; Combobox1.text = "male"; Combobox2.text = "18"; TextBox2.Text = ""; } }}
Operation Result: