C # Read and Write INI files (2)

Source: Internet
Author: User

You can call two APIs in kernel32.dll: writeprivateprofilestring and getprivateprofilestring to read INI files.

The specific implementation code is as follows:

 
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. runtime. interopservices; // dllimport
Using system. Text; // stringbuilder

Namespace iniprocess
{

Public class form1: system. Windows. Forms. Form
{
Private system. Windows. Forms. textbox textbox1;
Private system. Windows. Forms. Button button2;
Private system. Windows. Forms. Button button1;

[Dllimport ("Kernel32")]
Private Static extern long writeprivateprofilestring (string section,
String key, string Val, string filepath );
[Dllimport ("Kernel32")]
Private Static extern int getprivateprofilestring (string section,
String key, string def, stringbuilder retval,
Int size, string filepath );

Public void iniwritevalue (string section, string key, string value, string filepath) // The write operation on the INI file.
{
Writeprivateprofilestring (section, key, value, filepath );
}

Public String inireadvalue (string section, string key, string filepath) // a function for reading an INIFILE
{
Stringbuilder temp = new stringbuilder (255 );
Int I = getprivateprofilestring (section, key, "", temp,
255, filepath );
Return temp. tostring ();

}

Private void button#click (Object sender, system. eventargs E)
{

This. textbox1. Text = inireadvalue ("ODBC 32 bit data sources", "MS Access Database", "E: \ temp \ ODBC. ini ");

}

Private void button2_click (Object sender, system. eventargs E)
{

Iniwritevalue ("ODBC 32 bit data sources", "MS Access Database", this. textbox1. Text, "E: \ temp \ ODBC. ini ");
}
}
}

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.