您好,登錄后才能下訂單哦!
這篇文章主要介紹了C#操作ini文件的幫助類是什么的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇C#操作ini文件的幫助類是什么文章都會有所收獲,下面我們一起來看看吧。
using System; using System.Runtime.InteropServices; using System.Text; namespace IniDemo { public class IniFile { private string m_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, null, null, this.m_FileName); } public string IniReadValue(string section, string name) { StringBuilder stringBuilder = new StringBuilder(256); 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); } } }
IniFile iniFile = new IniFile(Environment.CurrentDirectory + "\\LocalInf.ini"); //讀取Local節點下M的值,默認為空值 string m = iniFile.ReadString("Local", "M", ""); //Local節點下寫F=f iniFile.WriteString("Local", "F", "f"); //讀取Local節點下IsSleep的字符串值,并轉為bool類型值,給出默認值為False bool f = bool.Parse(iniFile.ReadString("Local", "IsSleep", "False")); //讀取Local節點下的C的字符串值,并轉為double類型值,給出默認值0 bool f = double.Parse(iniFile.ReadString("Local", "C", "0"));
關于“C#操作ini文件的幫助類是什么”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“C#操作ini文件的幫助類是什么”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。