Usually C# uses XML-based configuration files, but if necessary, for example, to take into account older systems, INI files may still be used.
However, C# itself does not have an API for reading and writing INI files. Only by calling unmanaged code, that is, the system's own API, can the desired goal be achieved.
The corresponding reading and writing methods are GetPrivateProfileString and WritePrivateProfileString respectively.
Each parameter in GetPrivateProfileString:
lpAppName ——The name of section
lpKeyName - the name of the key
lpDefault — If lpKeyName is not found, copy this value into lpReturnedString
lpReturnedString — the value used to return the result
nSize —— Character length of lpReturnedString
lpFileName —— INI file name
Each parameter in WritePrivateProfileString:
lpAppName ——The name of section
lpKeyName - the name of the key
lpString — the value corresponding to lpKeyName
lpFileName —— INI file name
The actual code looks like this:
using System; using ; using ; namespace INIDemo { class Program { static void Main(string[] args) { WritePrivateProfileString("Demo", "abc", "123", "c:\\"); StringBuilder temp = new StringBuilder(); GetPrivateProfileString("Demo", "abc", "", temp, 255, "c:\\"); (temp); (); } [DllImport("kernel32", CharSet = , SetLastError = true)] private static extern bool WritePrivateProfileString( string lpAppName, string lpKeyName, string lpString, string lpFileName); [DllImport("kernel32", CharSet = , SetLastError = true)] private static extern int GetPrivateProfileString( string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); } }
After the program runs, the contents in the INI file are:
[Demo]
abc=123
This is a relatively simple method. If you don't want to use the unmanaged method, you can also solve this problem in another troublesome way.
Implement code 2.
public class Ini { // Declare the write operation function of INI file WritePrivateProfileString() [("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); // Declare the read operation function of INI file GetPrivateProfileString() [("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, retVal, int size, string filePath); private string sPath = null; public Ini(string path) { = path; } public void Writue(string section, string key, string value) { // section=configuration section, key=key name, value=key value, path=path WritePrivateProfileString(section, key, value, sPath); } public string ReadValue(string section, string key) { // How many bytes are read from the ini each time temp = new (255); // section=config section, key=key name, temp=above, path=path=path GetPrivateProfileString(section, key, "", temp, 255, sPath); return (); } }
Since the format of the INI file is fixed, the same read and write functions can be completed by writing the corresponding parsing program, which is just 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, which can achieve what you want to do for you.
Then everything became simple again.