1. First import the namespace: using System. Runtime. InteropServices;
2. Declare API functions
[DllImport ("kernel32")]
Private static extern long WritePrivateProfileString (string section, string key, string val, string filePath );
3. Create an INI File
WritePrivateProfileString ("MyQQ", "ID", "798033502", @ "C: \ QQ. ini ");
WritePrivateProfileString ("MyQQ", "PWD", "***********", @ "C: \ QQ. ini ");
4. Declare API functions
[DllImport ("kernel32")]
Private static extern int GetPrivateProfileString (string section, string key, string def, StringBuilder retVal, int size, string filePath );
5. Read the INI File
StringBuilder temp = new StringBuilder ();
GetPrivateProfileString ("MyQQ", "ID", "Account Error", temp, 255, @ "C: \ QQ. ini ");
String ID = temp. ToString ();
GetPrivateProfileString ("MyQQ", "PWD", "read error", temp, 255, @ "C: \ QQ. ini ");
String PWD = temp. ToString ();
MessageBox. Show (ID + ":" + PWD );
6 The complete code is as follows:
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. Runtime. InteropServices;
Namespace WindowsFormsApplication1
{
Public partial class Main: Form
{
Public Main ()
{
InitializeComponent ();
}
[DllImport ("kernel32")]
Private static extern long WritePrivateProfileString (string section, string key, string val, string filePath );
Private void button#click (object sender, EventArgs e)
{
WritePrivateProfileString ("MyQQ", "ID", "798033502", @ "C: \ QQ. ini ");
WritePrivateProfileString ("MyQQ", "PWD", "***********", @ "C: \ QQ. ini ");
}
[DllImport ("kernel32")]
Private static extern int GetPrivateProfileString (string section, string key, string def, StringBuilder retVal, int size, string filePath );
Private void btnRead_Click (object sender, EventArgs e)
{
StringBuilder temp = new StringBuilder (); www.2cto.com
GetPrivateProfileString ("MyQQ", "ID", "Account Error", temp, 255, @ "C: \ QQ. ini ");
String ID = temp. ToString ();
GetPrivateProfileString ("MyQQ", "PWD", "read error", temp, 255, @ "C: \ QQ. ini ");
String PWD = temp. ToString ();
MessageBox. Show (ID + ":" + PWD );
}
}
}
Author: MyPC2010