The .ini file is the abbreviation of Initialization File, that is, the initialization file, which is the storage format used by Windows' system configuration files. It manages the various configurations of Windows. Generally, users can use the graphical management interface provided by Windows to achieve the same configuration. However, in some cases, it is still convenient to edit the ini directly. Generally, only if you are familiar with Windows, you can edit it directly.
1. Define Class
using System; using ; using ; namespace IniDemo { public class IniFile { private string m_FileName; public string FileName { get { return this.m_FileName; } set { this.m_FileName = value; } } [DllImport("")] private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName); [DllImport("")] private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); [DllImport("")] private static extern int WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName); public IniFile(string aFileName) { this.m_FileName = aFileName; } public IniFile() { } public int ReadInt(string section, string name, int def) { return (section, name, def, this.m_FileName); } public string ReadString(string section, string name, string def) { StringBuilder stringBuilder = new StringBuilder(2048); (section, name, def, stringBuilder, 2048, this.m_FileName); return (); } public void WriteInt(string section, string name, int Ival) { (section, name, (), this.m_FileName); } public void WriteString(string section, string name, string strVal) { (section, name, strVal, this.m_FileName); } public void DeleteSection(string section) { (section, null, null, this.m_FileName); } public void DeleteAllSection() { (null, null, null, this.m_FileName); } public string IniReadValue(string section, string name) { StringBuilder stringBuilder = new StringBuilder(256); (section, name, "", stringBuilder, 256, this.m_FileName); return (); } public void IniWriteValue(string section, string name, string value) { (section, name, value, this.m_FileName); } } }
2. Calling methods
IniFile iniFile = new IniFile( + "\\"); //Read the value of M under the Local node, the default is emptystring m = ("Local", "M", ""); //Write F=f under the Local node("Local", "F", "f"); //Read the string value of IsSleep under the Local node and convert it to a bool type value, giving the default value as False bool f = (("Local", "IsSleep", "False")); //Read the string value of C under the Local node and convert it to a double type value, giving the default value 0 bool f = (("Local", "C", "0"));
This is all about this article about C# operation ini file help class. I hope it will be helpful to everyone's learning and I hope everyone will support me more.