C # create and read an INI File

Source: Internet
Author: User

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

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.