Our C # Read and write configuration files are usually read and written to files with IO. I think using WINAPI's Kernel32.dll to read INI file is better and more convenient
Read and write to INI file we can use the WritePrivateProfileString (write) in Kernel32.dll, GetPrivateProfileString (read in string), Getprivateprofileint (Read integer value) three methods. I'm here to introduce three methods.
I. Write information to the. ini file.
1. The WINAPI function used is: writeprivateprofilestring
[DllImport ("Kernel32.dll")]
public static extern bool WritePrivateProfileString (string strappname,
String Strkeyname,
String strstring,
String strFileName);
The meaning of each of these parameters:
String Strappname, is a field name in the INI file.
String Strkeyname, is a strappname under the name of a key, popularly speaking is the variable name.
String strstring, is a key value, which is the value of the variable (string type)
String strFileName is the full INI file path name.
2. The specific use method: I have a Web page refresh device. I want to save the Web page (URL) that I set up, and the interval (time)
WritePrivateProfileString ("Settings", "Url", "http://wenti.cnblogs.com", "C:\\config.ini");
WritePrivateProfileString ("Settings", "Time", "a", "C:\\config.ini");
The contents of the saved INI file are as follows:
[Settings]
Url=http://wenti.cnblogs.com
Time=20