[C #]: How to read INI files in C # (2)

zhaozj2021-02-16  47

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 sext.com2.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 ();

}

转载请注明原文地址:https://www.9cbs.com/read-22180.html

New Post(0)