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");
}
}
}