Read the core code of the configuration file and the write configuration file:
[DllImport ("kernel32")]//Read the 6 parameters of the configuration file method: the partition (section), the key value, the initial default value, the StringBuilder, Maximum parameter length, config file path private static extern int getprivateprofilestring (string section, string key, String deval, Stringbui Lder retVal, int size, string filePath); [DllImport ("kernel32")]//4 parameters of the Write configuration file method: the partition (section), the key value, the parameter value, the configuration file path private static extern long WritePrivateProfileString (string section, string key, String val, string filePath); public static void SetValue (string section, string key, String value) {//Gets the current path, which is currently under the debug path String strpath = Environment.currentdirectory + "\\system.ini"; WritePrivateProfileString (section, key, value, strpath); } public static string GetValue (string section, string key) {StringBuilder SB = new Stringbuilde R (255); String strpath = environment.currentdirectory + "\ \System.ini "; It is best to set the initial default value to non-null, because if the configuration file does not exist, and the value is not available, the program will not error getprivateprofilestring (section, key, "configuration file does not exist, not taken to parameters", SB, 255, strpath ); Return SB. ToString (); }
"Application Examples"
Function Description: When the program loads, create the configuration file and write the baud rate parameter inside. (The configuration file does not need to exist beforehand, and this Windows API is created automatically). Click Button1 to display the baud rate to textBox1.
The complete code is as follows:
Using system;using system.codedom;using system.collections.generic;using system.componentmodel;using System.Data; Using system.drawing;using system.io;using system.linq;using system.runtime.interopservices;using System.Text;using system.threading.tasks;using system.windows.forms;namespace windowsformsapplication1{public partial class Form1:for m {public Form1 () {InitializeComponent (); } [DllImport ("kernel32")]//Read the 6 parameters of the profile method: the partition (section), the key value, the initial default value, the Stringbui Lder, maximum parameter length, configuration file path private static extern int getprivateprofilestring (string section, string key, String deval, S Tringbuilder retVal, int size, string filePath); [DllImport ("kernel32")]//4 parameters of the Write configuration file method: the partition (section), the key value, the parameter value, the configuration file path private static extern long WritePrivateProfileString (string section, string key, String val, string filePath); public static Void SetValue (string section, string key, String value) {//Gets the current path and is currently in the debug path under String strpath = Environment.currentdirectory + "\\system.ini"; WritePrivateProfileString (section, key, value, strpath); } public static string GetValue (string section, string key) {StringBuilder SB = new Stringbuilde R (255); String strpath = Environment.currentdirectory + "\\system.ini"; It is best to set the initial default value to non-null, because if the configuration file does not exist, and the value is not available, the program will not error getprivateprofilestring (section, key, "configuration file does not exist, not taken to parameters", SB, 255, strpath ); Return SB. ToString (); } private void Form1_Load (object sender, EventArgs e) {SetValue ("parameter", "baud rate", "9600"); } private void Button1_Click (object sender, EventArgs e) {TextBox1.Text = GetValue ("parameter Number "," baud rate "); } }}
Program Interface:
Read and write configuration parameter files in C # using Windows APIs