C # operate on the INI File

Source: Internet
Author: User

The INI file format is as follows:
[Database]
Server = wscsi
Database = mydatabase
Uid = sa
Pwd = 1, 123456

Note: (there are 4 keys)
Section: Database
Key: Server database uid pwd

 

 

C # declare the write operation function writeprivateprofilestring Of The INI file:

[Dllimport ("Kernel32")]
Private Static extern long writeprivateprofilestring (string
Section,
String key, string Val, string filepath );
Parameter description: Section in the INI file; key: keyword in the INI file; VAL: value of the keyword in the INI file; filepath: complete path and name of the INI file.

C # declare the INI file's read operation function getprivateprofilestring:

[Dllimport ("Kernel32")]
Private Static extern int getprivateprofilestring (string section,
String key, string def, stringbuilder retval,
Int size, string filepath );
Parameter description: Section: section name in the INI file; key: keyword in the INI file; Def: default value when the file cannot be read; retval: read value; Size: value size; filepath: the complete path and name of the INI file.

 

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. runtime. interopservices;
Using system. text;


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.