C#完成讀寫ini文件類實例。本站提示廣大學習愛好者:(C#完成讀寫ini文件類實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成讀寫ini文件類實例正文
本文實例講述了C#完成讀寫ini文件類。分享給年夜家供年夜家參考。詳細以下:
這個C#類封裝了對INI設置裝備擺設文件停止操作所需的各類函數,包含讀取鍵值、讀取鍵值、刪除段落等
using System; using System.Runtime.InteropServices; using System.Text; namespace DotNet.Utilities { /// <summary> /// INI文件讀寫類。 /// </summary> public class INIFile { public string path; public INIFile(string INIPath) { path = INIPath; } [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section,string key,string val,string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section,string key,string def, StringBuilder retVal,int size,string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath); /// <summary> /// 寫INI文件 /// </summary> /// <param name="Section"></param> /// <param name="Key"></param> /// <param name="Value"></param> public void IniWriteValue(string Section,string Key,string Value) { WritePrivateProfileString(Section,Key,Value,this.path); } /// <summary> /// 讀取INI文件 /// </summary> /// <param name="Section"></param> /// <param name="Key"></param> /// <returns></returns> public string IniReadValue(string Section,string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section,Key,"",temp, 255, this.path); return temp.ToString(); } public byte[] IniReadValues(string section, string key) { byte[] temp = new byte[255]; int i = GetPrivateProfileString(section, key, "", temp, 255, this.path); return temp; } /// <summary> /// 刪除ini文件下一切段落 /// </summary> public void ClearAllSection() { IniWriteValue(null,null,null); } /// <summary> /// 刪除ini文件下personal段落下的一切鍵 /// </summary> /// <param name="Section"></param> public void ClearSection(string Section) { IniWriteValue(Section,null,null); } } }
願望本文所述對年夜家的C#法式設計有所贊助。