Simple analysis of C # operations INI file

Source: Internet
Author: User
Tags ini read ini file

C # language is still more common things, here we mainly introduce C # to INI file operations, including the introduction of the INI file to write operations and so on.

C # operations on INI file

The INI file is written and is implemented through the "click" Event of the component Button2. One thing to note here is that when you invoke WritePrivateProfileString () to write to the INI file, you will overwrite this INI message if you have the same paragraph name and keyword in the INI file as the information you want to write. The following is a list of code for the "click" Event for the Button2 component:

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文件!" , "信息" ) ;
}

The correct reading of the INI must meet three prerequisites: The full path to the INI file, the paragraph name, and the keyword name. Otherwise, it will not read correctly. You can set the default value after reading is unsuccessful, in the following program, in order to visually set is "unable to read the corresponding value!" "String, the Read INI file is implemented through the Button3 component's Click event, and the following is its corresponding code list:

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 ,"无法读取对应数值!",
temp , 255 , FileName ) ;
//显示读取的数值 
textBox4.Text = temp.ToString( ) ;
}

The above describes C # to INI file operations

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.