Read and write INI files in C #

Source: Internet
Author: User
Tags ini int size read ini file tostring

There are no classes in C # that read and write INI files, but they are easy to implement with API functions.

The form code is as follows:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using System.Runtime.InteropServices;

Namespace Inifiledemo
{
public partial class Form1:form
{
/**//**//**////<summary>
Read INI file API function declaration
</summary>
<param name= "section" > subsection name </param>
<param name= "key" > Keyword </param>
<param name= "def" > Default value if not found </param>
<param name= "RetVal" > buffer to store return values </param>
<param name= "Size" > Buffer size </param>
<param name= "FilePath" > Path to the INI file to read </param>
<returns> returns the number of characters read to </returns>
[DllImport ("kernel32")]
        private static extern long GetPrivateProfileString (string section, string Key, String def, StringBuilder retVal, int size, string filePath);

       /**//**//**////<summary>
        ///Write INI file API function declaration
       ///</ Summary>
       ///<param name= "section" > subsection name </param>
       ///<param name= "key" > Keywords </param>
        ///<param name= "val" > The value to be written </param>
        ///<param name= "FilePath" > Path </param> for the INI file to be written;
       / <returns> returns the number of characters written </returns>
        [DllImport ("kernel32")]
        private static extern long WritePrivateProfileString (string section, String key, String val, string filePath);

Public Form1 ()
{
InitializeComponent ();
}

/**//**//**////<summary>
Read the value from the INI file and apply it to the Width,height property of the form
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Form1_Load (object sender, EventArgs e)
{
StringBuilder mybuilder=new StringBuilder (256);
GetPrivateProfileString ("Settings", "width", "Mybuilder", "256", ". App.ini");
Width = Int32.Parse (mybuilder.tostring ());
TextBox1.Text = Width.tostring ();

GetPrivateProfileString ("Settings", "height", "Mybuilder", "256", ". App.ini");
Height = Int32.Parse (mybuilder.tostring ());
TextBox2.Text = Height.tostring ();
}

/**//**//**////<summary>
Write a custom value to the INI file
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button1_Click (object sender, EventArgs e)
{
Width = Int32.Parse (TextBox1.Text);
Height = Int32.Parse (TextBox2.Text);
WritePrivateProfileString ("Settings", "width", TextBox1.Text, ". App.ini");
WritePrivateProfileString ("Settings", "height", TextBox2.Text, ". App.ini");
}
}
}



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.