The main idea is to invoke the Win32 API.
1. Introducing namespaces
Using System.Runtime.InteropServices;
2. Declaration (Turn a WIN32 API function into a C # function)
//声明INI文件的写操作函数 WritePrivateProfileString()
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
//声明INI文件的读操作函数 GetPrivateProfileString()
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
3. Call
//write a config.ini file
private void Save_ini ()
{
String s = System.Windows.Forms.Application.Executabl Epath;
//In the current directory, write a config.ini file
string path = S.tolower (). Replace ("Mediaconvert.exe", "Config.ini");
String configurenode = "databaseconfigure";//configuration section
String key1 = "DataBase";/Key Name
String key1_value = "Databa SeName ";//Key value
String key2 =" Server ";
String key2_value = "ServerName";
String Key3 = "UserId";
String key3_value = "1";
WritePrivateProfileString (Configurenode, Key1, Key1_value, Path);
WritePrivateProfileString (Configurenode, Key2, Key2_value, Path);
WritePrivateProfileString (Configurenode, Key3, Key3_value, Path);
/* Finally, in the same directory as the EXE file, generate a config.ini file, which should read as follows:
* [Databaseconfigure]
* Database=databasename
* server=serve Rname
* userid=1
*/
}//Read the configuration in the Config.ini file
private void Read_ini ()
{
String s = System. Windows.Forms.Application.ExecutablePath;
//Get COnfig.ini path
String path = S.tolower (). Replace ("Mediaconvert.exe", "Config.ini");
StringBuilder str = new StringBuilder (255);
//Get the value of the database key for configuration section [Databaseconfigure]
GetPrivateProfileString ("Databaseconfigure", "DataBase", "", str, 255, path); The result in the
//dialog box should be Database:databasename
System.Windows.Forms.MessageBox.Show ("DataBase:" + str.) ToString ());
}
C # uses the system API, the most troublesome problem is the data type in the API in C #, how to correspond to the problem.
This site (www.pinvoke.net) lists most of the system APIs in this C # or vb.net statement, in detail.