1. 程式人生 > >C#類庫:ini檔案操作類

C#類庫:ini檔案操作類

C#類庫

——ini檔案操作類

1.類庫介紹

    在開發應用軟體時,ini檔案常用於軟體的相關配置,以下為ini檔案的相關結構及示例;ini檔案具有節(section)和鍵(key)兩個層級,節用”[]”包含,然後下一行為對應鍵名以及鍵值,在示例中,“[INFO]”為節,“Name”和“Age”為鍵名,“SWorld”和“0”為鍵值。

[section]
key=value

例:
[INFO]
Name=SWorld
Age=0

    類庫引用:

using System.IO;
using System.Runtime.InteropServices;
using
System.Text;
2.類庫原始碼
/// <summary>
/// ini檔案操作類
/// </summary>
public class IniHelper
{
    #region 動態連結庫呼叫
    /// <summary>
    /// 呼叫動態連結庫讀取值
    /// </summary>
    /// <param name="lpAppName">ini節名</param>
    /// <param name="lpKeyName">ini鍵名</param>
    /// <param name="lpDefault">
預設值:當無對應鍵值,則返回該值。</param>
/// <param name="lpReturnedString">結果緩衝區</param> /// <param name="nSize">結果緩衝區大小</param> /// <param name="lpFileName">ini檔案位置</param> /// <returns></returns> [DllImport("kernel32")] private static extern int GetPrivateProfileString
( string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); /// <summary> /// 呼叫動態連結庫寫入值 /// </summary> /// <param name="mpAppName">ini節名</param> /// <param name="mpKeyName">ini鍵名</param> /// <param name="mpDefault">寫入值</param> /// <param name="mpFileName">檔案位置</param> /// <returns>0:寫入失敗 1:寫入成功</returns> [DllImport("kernel32")] private static extern long WritePrivateProfileString( string mpAppName, string mpKeyName, string mpDefault, string mpFileName); #endregion /// <summary> /// 讀ini檔案 /// </summary> /// <param name="section"></param> /// <param name="key"></param> /// <returns>返回讀取值</returns> public static string Ini_Read(string section, string key, string path) { StringBuilder stringBuilder = new StringBuilder(1024); //定義一個最大長度為1024的可變字串 GetPrivateProfileString(section, key, "", stringBuilder, 1024, path); //讀取INI檔案 return stringBuilder.ToString(); //返回INI檔案的內容 } /// <summary> /// 寫ini檔案 /// </summary> /// <param name="section"></param> /// <param name="key"></param> /// <param name="iValue">待寫入值</param> public static void Ini_Write(string section, string key, string iValue, string path) { WritePrivateProfileString(section, key, iValue, path); //寫入 } /// <summary> /// 根據檔名建立檔案 /// </summary> /// <param name="path">檔名稱以及路徑</param> public static void ini_creat(string path) { if (!File.Exists(path)) //判斷是否存在相關檔案 { FileStream _fs=File.Create(path); //不存在則建立ini檔案 _fs.Close(); //關閉檔案,解除佔用 } } /// <summary> /// 刪除ini檔案中鍵 /// </summary> /// <param name="section">節名稱</param> /// <param name="key">鍵名稱</param> /// <param name="path">ini檔案路徑</param> public static void Ini_Del_Key(string section,string key, string path) { WritePrivateProfileString(section, key, null, path); //寫入 } /// <summary> /// 刪除ini檔案中節 /// </summary> /// <param name="section">節名</param> /// <param name="path">ini檔案路徑</param> public static void Ini_Del_Section(string section, string path) { WritePrivateProfileString(section, null, null, path); //寫入 } }
3.類庫使用
#region INI操作類測試
string _str_path = Path.Combine(@"G:", "ini_test.ini");             //路徑
Console.WriteLine("————Create File:");
IniHelper.Ini_Create(_str_path);                                    //在G盤建立名為ini_test的ini檔案

Console.WriteLine("————Write Name:");
IniHelper.Ini_Write("INFO", "Name", "Test", _str_path);             //在路徑檔案中寫入節為“INFO”,鍵名為“Name”,鍵值為“SWorld”的資料
Console.WriteLine("————Write Age:");
IniHelper.Ini_Write("INFO", "Age", "0", _str_path);                 //在路徑檔案中寫入節為“INFO”,鍵名為“Age”,鍵值為“0”的資料

Console.WriteLine("————Read Name:");
string _str_Name = IniHelper.Ini_Read("INFO", "Name", _str_path);   //讀取Name
Console.WriteLine("Name:"+_str_Name);                               //列印Name
Console.WriteLine("————Read Age:");
string _str_Age = IniHelper.Ini_Read("INFO", "Age", _str_path);     //讀取Age
Console.WriteLine("Age:" + _str_Age);                               //列印Age

Console.WriteLine("————Update Name:");
IniHelper.Ini_Write("INFO", "Name", "SWorld", _str_path);           //更新資料
Console.WriteLine("————Read New Name:");
_str_Name = IniHelper.Ini_Read("INFO", "Name", _str_path);          //讀取Name
Console.WriteLine("Name:" + _str_Name);                             //列印Name

Console.WriteLine("————Delete Key Name:");
IniHelper.Ini_Del_Key("INFO", "Name", _str_path);                   //刪除鍵
Console.WriteLine("————Read Name And Age:");
_str_Name = IniHelper.Ini_Read("INFO", "Name", _str_path);          //讀取Name
Console.WriteLine("Name:" + _str_Name);                             //列印Name
_str_Age = IniHelper.Ini_Read("INFO", "Age", _str_path);            //讀取Age
Console.WriteLine("Age:" + _str_Age);                               //列印Age

Console.WriteLine("————Delete Section Info:");
IniHelper.Ini_Del_Section("INFO", _str_path);                       //刪除節
Console.WriteLine("————Read Name And Age:");
_str_Name = IniHelper.Ini_Read("INFO", "Name", _str_path);          //讀取Name
Console.WriteLine("Name:" + _str_Name);                             //列印Name
_str_Age = IniHelper.Ini_Read("INFO", "Age", _str_path);            //讀取Age
Console.WriteLine("Age:" + _str_Age);                               //列印Name
#endregion

結果:
這裡寫圖片描述

【注意】:從結果可以看出,再次寫入鍵值會覆蓋原來鍵值,需要刪除對應的鍵或者節,則只需要將對應節或者鍵寫入null就行。