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