What is the help class for C # to manipulate ini files
This article mainly introduces the C # operation of the ini file help class is what the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that everyone after reading this C# operation ini file help class is what the article will have a harvest, let's take a look at it.
First, define Classusing System;using System.Runtime.InteropServices;using System.Text;namespace IniDemo {public class IniFile {private string masked FileName; public string FileName {get {return this.m_FileName } set {this.m_FileName = value;}} [DllImport ("kernel32.dll")] private static extern int GetPrivateProfileInt (string lpAppName, string lpKeyName, int nDefault, string lpFileName) [DllImport ("kernel32.dll")] private static extern int GetPrivateProfileString (string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); [DllImport ("kernel32.dll")] 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 IniFile.GetPrivateProfileInt (section, name, def, this.m_FileName) } public string ReadString (string section, string name, string def) {StringBuilder stringBuilder = new StringBuilder (2048); IniFile.GetPrivateProfileString (section, name, def, stringBuilder, 2048, this.m_FileName); return stringBuilder.ToString () } public void WriteInt (string section, string name, int Ival) {IniFile.WritePrivateProfileString (section, name, Ival.ToString (), this.m_FileName) } public void WriteString (string section, string name, string strVal) {IniFile.WritePrivateProfileString (section, name, strVal, this.m_FileName);} public void DeleteSection (string section) {IniFile.WritePrivateProfileString (section, null, null, this.m_FileName) } public void DeleteAllSection () {IniFile.WritePrivateProfileString (null, this.m_FileName);} public string IniReadValue (string section, string name) {StringBuilder stringBuilder = new StringBuilder IniFile.GetPrivateProfileString (section, name, ", stringBuilder, 256, this.m_FileName); return stringBuilder.ToString ();} public void IniWriteValue (string section, string name, string value) {IniFile.WritePrivateProfileString (section, name, value, this.m_FileName) 2. Call the method IniFile iniFile = new IniFile (Environment.CurrentDirectory + "\\ LocalInf.ini"); / / read the value of M under the Local node, default to null value string m = iniFile.ReadString ("Local", "M", ""); / / write F=finiFile.WriteString ("Local", "F", "f") under the Local node / / read the string value of IsSleep under the Local node and convert it to the bool type value, and give the default value False bool f = bool.Parse ("Local", "IsSleep", "False"); / / read the string value of C under the Local node and convert it to the double type value, giving the default value 0 bool f = double.Parse ("Local", "C", "0")) This is the end of the article on "what are the help classes for operating ini files in C#?" Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the help class for operating ini files in C#". If you want to learn more knowledge, you are welcome to follow the industry information channel.