C # Read and Write INI files

Source: Internet
Author: User

Because I have always wanted to develop a software, so I am working hard! The structure of the INI file is a text file that is arranged according to the characteristics. Each INI file is very similar and consists of several sections. Under each title with parentheses, there are several keywords starting with a single word (keyword) and an equal sign, the right side of the equal sign is the value corresponding to the keyword ). The general format is as follows: [Section1] KeyWord1 = ValuelKeyWord2 = Value2 ...... [Section2] KeyWord3 = Value3KeyWord4 = Value42.C # And Win32 API Function C # do not have their own class libraries like C ++. C # The class library used is a common class library provided by the. Net FrameWork for all. Net Program Development-. Net FrameWork SDK. Although the. Net FrameWork SDK has a huge amount of content and powerful functions, it cannot cover all aspects. At least it does not provide the relevant classes required to directly operate the INI file. In this article, C # uses the Win32 API function -- WritePrivateProfileString () and GetPrivateProfileString () in Windows to operate the INI file. C # declare the write operation function WritePrivateProfileString () of the INI file: [DllImport ("kernel32")] private static extern long WritePrivateProfileString (stringsection, string key, string val, string filePath ); parameter description: section in the INI file; key: keyword in the INI file; val: value of the keyword in the INI file; filePath: complete path and name of the INI file. C # declare the INI file's read operation function GetPrivateProfileString (): [DllImport ("kernel32")] private static extern int GetPrivateProfileString (string section, string key, string def, StringBuilder retVal, int size, string filePath); parameter description: section: paragraph name in the INI file; key: keyword in the INI file; def: default value when the file cannot be read; retVal: read value; size: the value. filePath: the complete path and name of the INI file. # Operation time code: // open the specified INI file private void button#click (object sender, System. eventArgs e) {try {openFileDialog1.ShowDialog (); textBox1.Text = openFileDialog1.FileName;} catch (Exception ex) {MessageBox. show (ex. message) ;}}// write the INI file private void button2_Click (object sender, System. eventArgs e) {string FileName = textBox1.Text; string section = textBox2.Text; string key = textBox3.Text; string key Value = textBox4.Text; try {// write the INI File Attribute WritePrivateProfileString (section, key, keyValue, FileName); MessageBox. Show ("the INI file is successfully written !", "Information");} catch (Exception ex) {MessageBox. show (ex. message) ;}}// read the value of the keyword 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; // read the INI file property value-grant the current property temp int I = GetPrivateProfileString (section, key, "cannot read the corresponding value! ", Temp, 255, FileName); // display the read value textBox4.Text = temp. toString ();} classic demo: The INI file is generally used to save the currently running program or some files with temporary configuration attributes. It is also sometimes used to save certain data for temporary or configuration needs. The text format is as follows: [Section1 Name] --------- is enclosed by []. It contains multiple key KeyName1 = value1 ------ and the format is Key = value. KeyName2 = value2... [Section2 Name] KeyName1 = value1 KeyName2 = value2C # operation time code: // open the specified INI file private void button#click (object sender, System. eventArgs e) {try {openFileDialog1.ShowDialog (); textBox1.Text = openFileDialog1.FileName;} catch (Exception ex) {MessageBox. show (ex. message) ;}}// write the INI file private void button2_Click (object sender, System. eventArgs e) {string FileName = textBox1.Text; st Ring section = textBox2.Text; string key = textBox3.Text; string keyValue = textBox4.Text; try {// write the INI file attributes WritePrivateProfileString (section, key, keyValue, FileName); MessageBox. show ("the INI file is successfully written! ", "Information");} catch (Exception ex) {MessageBox. show (ex. message) ;}}// read the value of the keyword 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; // read the INI file property value-grant the current property temp int I = GetPrivateProfileString (section, key, "cannot read the corresponding value! ", Temp, 255, FileName); // display the read value textBox4.Text = temp. ToString ();}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.