C # Read and write INI file

Source: Internet
Author: User
Tags config ini int size win32

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.

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.