關於C#操作INI文件的總結

INI文件其實是一種具有特定結構的文本文件,它的構成分爲三部分,結構如下:

[Section1]
key 1 = value2
key 1 = value2
……
[Section2]
key 1 = value1
key 2 = value2
……

文件由若干個段落(section)組成,每個段落又分成若干個鍵(key)和值(value)。Windows系統自帶的Win32的API函數GetPrivateProfileString()和WritePrivateProfileString()分別實現了對INI文件的讀寫操作,他們位於kernel32.dll下。

但是令人遺憾的是C#所使用的.NET框架下的公共類庫並沒有提供直接操作INI文件的類,所以唯一比較理想的方法就是調用API函數。

然後,.Net框架下的類庫是基於託管代碼的,而API函數是基於非託管代碼的,(在運行庫的控制下執行的代碼稱作託管代碼。相反,在運行庫之外運行的代碼稱作非託管代碼。)如何實現託管代碼與非託管代碼之間的操作呢?.Net框架的System.Runtime.InteropServices命名空間下提供各種各樣支持COM interop及平臺調用服務的成員,其中最重要的屬性之一DllImportAttribute可以用來定義用於訪問非託管API的平臺調用方法,它提供了對從非託管DLL導出的函數進行調用所必需的信息。下面就來看一下如何實現C#與API函數的互操作。

讀操作:


[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath);
section:要讀取的段落名
key: 要讀取的鍵
defVal: 讀取異常的情況下的缺省值
retVal: key所對應的值,如果該key不存在則返回空值
size: 值允許的大小
filePath: INI文件的完整路徑和文件名
寫操作:


[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
section: 要寫入的段落名
key: 要寫入的鍵,如果該key存在則覆蓋寫入
val: key所對應的值
filePath: INI文件的完整路徑和文件名
      

這樣,在就可以使用對他們的調用,用常規的方式定義一個名爲IniFile類:


using System;
using System.Runtime.InteropServices;
using System.Text;

namespace IPVOD.Hotel.Remoting
{
    /// <summary>
    /// INI文件的操作類
    /// </summary>
    public class IniFile
    {
        public string Path;

        public IniFile(string path)
        {
            this.Path = path;
        }
       
        聲明讀寫INI文件的API函數#region 聲明讀寫INI文件的API函數
        [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 defVal, 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="iValue">值</param>
        public void IniWriteValue(string section, string key, string iValue)
        {
            WritePrivateProfileString(section, key, iValue, 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();
        }

        /// <summary>
        /// 讀取INI文件
        /// </summary>
        /// <param name="Section">段,格式[]</param>
        /// <param name="Key">鍵</param>
        /// <returns>返回byte類型的section組或鍵值組</returns>
        public byte[] IniReadValues(string section, string key)
        {
            byte[] temp = new byte[255];

            int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path);
            return temp;
        }
    }
}
 
注意:增加了DLL導出的函數GetPrivateProfileString的重載,說明如下:

 

[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);
section:要讀取的段落名
key: 要讀取的鍵
defVal: 讀取異常的情況下的缺省值
retVal: 此參數類型不是string,而是Byte[]用於返回byte類型的section組或鍵值組。
size: 值允許的大小
filePath: INI文件的完整路徑和文件名

下面看一下具體實例化IniFile類的操作:

//path爲ini文件的物理路徑
IniFile ini = new IniFile(path);

//讀取ini文件的所有段落名
byte[] allSection = ini.IniReadValues(null, null);
通過如下方式轉換byte[]類型爲string[]數組類型
string[] sectionList;
ASCIIEncoding ascii = new ASCIIEncoding();

//獲取自定義設置section中的所有key,byte[]類型
sectionByte = ini.IniReadValues("personal", null);

//編碼所有key的string類型
sections = ascii.GetString(sectionByte);

//獲取key的數組
sectionList = sections.Split(new char[1]{'/0'});

//讀取ini文件personal段落的所有鍵名,返回byte[]類型
byte[] sectionByte = ini.IniReadValues("personal", null);

//讀取ini文件evideo段落的MODEL鍵值
model = ini.IniReadValue("evideo", "MODEL");

//將值eth0寫入ini文件evideo段落的DEVICE鍵
ini.IniWriteValue("evideo", "DEVICE", "eth0");
即:
[evideo]
DEVICE = eth0

//刪除ini文件下personal段落下的所有鍵
ini.IniWriteValue("personal", null, null);

//刪除ini文件下所有段落
ini.IniWriteValue(null, null, null);
這樣就實現了C#對ini文件包括段落section,鍵key,鍵值value的基本上所有操作

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章