C # usually uses an xml-based configuration file, but if necessary, such as taking care of older systems, you might want to use the INI file.
But C # itself does not have the API to read and write the INI file, only by invoking unmanaged code, the system's own APIs to achieve the desired purpose.
The corresponding methods for reading and writing are getprivateprofilestring and writeprivateprofilestring respectively.
The parameters in the GetPrivateProfileString:
Name of Lpappname--section
Name of Lpkeyname--key
lpdefault--if Lpkeyname is not found, copy this value into lpreturnedstring
lpreturnedstring--value used to return the result
Character length of nsize--lpreturnedstring
Lpfilename--ini file name
The parameters in the WritePrivateProfileString:
Name of Lpappname--section
Name of Lpkeyname--key
lpstring--values corresponding to Lpkeyname
Lpfilename--ini file name
The actual code looks like this:
Using System;
Using System.Runtime.InteropServices;
Using System.Text; Namespace Inidemo {class Program {static void Main (string[] args) {writ
Eprivateprofilestring ("Demo", "abc", "123", "C:\\demo.ini");
See more highlights of this column: Http://www.bianceng.cn/Programming/csharp/StringBuilder temp = new StringBuilder ();
GetPrivateProfileString ("Demo", "abc", "" ", temp, 255," C:\\demo.ini ");
Console.WriteLine (temp);
Console.ReadLine (); } [DllImport ("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] private static extern
BOOL WritePrivateProfileString (String lpappname, String lpkeyname, String lpstring, string lpfilename); [DllImport ("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] private static extern in T GetPrivateProfileString (String lpappname, String LpkeYname, String lpdefault, StringBuilder lpreturnedstring, int nsize, string lpfilename); }
}
The content in the INI file after the program runs is:
[Demo]
Abc=123
This is a relatively simple approach, and if you don't want to use an unmanaged method, you can solve the problem in a different, more troublesome way.
Because the format of the INI file is fixed, so long as the corresponding parsing program can complete the same read and write functions, is the usual string processing.
If you don't want to do it yourself, it doesn't matter, there is already a ready-made program--cinchoo framework that can help you achieve what you want to do.
And then it all gets easier.