C# ini文件操做【源碼下載】

  介紹C#如何對ini文件進行讀寫操做,C#能夠經過調用【kernel32.dll】文件中的 WritePrivateProfileString()和GetPrivateProfileString()函數分別對ini文件進行讀和寫操做。包括:讀取key的值、保存key的值、讀取全部section、讀取全部key、移除section、移除key等操做。html

目錄

1. ini文件介紹web

2. 讀取操做:包括讀取key的值、讀取全部section、讀取全部key等操做。app

3. 寫入操做: 包括保存key的值、移除section、移除key等操做。函數

4. 源碼下載:展現運行圖及源碼下載spa

 

1. ini文件介紹

ini文件經常使用於存儲各種應用的配置信息,而內部的文件結構主要包括三個概念:sectionkeyvalue.net

其中section爲各獨立的區域塊,名稱能夠爲英文、中文。htm

 

2. GetPrivateProfileString()函數 :讀取操做

C#能夠經過調用【kernel32.dll】文件中的 GetPrivateProfileString()函數對ini文件進行讀取操做。blog

官方APIhttps://msdn.microsoft.com/zh-cn/library/ms724353.aspxip

函數簽名rem

[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string sectionName, string key, string defaultValue, byte[] returnBuffer, int size, string filePath); 

成員

sectionName  {string | null}:要讀區的區域名。若傳入null值,第4個參數returnBuffer將會得到全部的section name。

key {string | null}:key的名稱。若傳入null值,第4個參數returnBuffer將會得到全部的指定sectionName下的全部key name。

defaultValue {string}:key沒找到時的返回值。

returnBuffer {byte[]}:key所對應的值。

filePath {string}:ini文件路徑。

支持的操做

1) 獲取指定key的值

2) 獲取ini文件全部的section名稱

3) 獲取指定section下的全部key名稱

 

2.1 獲取指定key的值

/// <summary>
/// 根據Key讀取Value
/// </summary>
/// <param name="sectionName">section名稱</param>
/// <param name="key">key的名稱</param>
/// <param name="filePath">文件路徑</param>
public static string GetValue(string sectionName, string key, string filePath)
{
    byte[] buffer = new byte[2048];
    int length = GetPrivateProfileString(sectionName, key, "發生錯誤", buffer,999, filePath);
    string rs = System.Text.UTF8Encoding.Default.GetString(buffer, 0, length);
    return rs;
}

 

2.2 獲取ini文件全部的section名稱

注意:中文名稱的section要進行轉碼。

/// <summary>
/// 獲取ini文件內全部的section名稱
/// </summary>
/// <param name="filePath">文件路徑</param>
/// <returns>返回一個包含section名稱的集合</returns>
public static List<string> GetSectionNames(string filePath)
{
    byte[] buffer = new byte[2048];
    int length = GetPrivateProfileString(null, "", "", buffer, 999, filePath);
    String[] rs = System.Text.UTF8Encoding.Default.GetString(buffer, 0, length).Split(new string[] { "\0" },StringSplitOptions.RemoveEmptyEntries);
    return rs.ToList();
}

  

2.3 獲取指定section下的全部key名稱

一樣要對中問名稱的key進行轉碼。

/// <summary>
/// 獲取指定section內的全部key
/// </summary>
/// <param name="sectionName">section名稱</param>
/// <param name="filePath">文件路徑</param>
/// <returns>返回一個包含key名稱的集合</returns>
public static List<string> GetKeys(string sectionName, string filePath)
{
    byte[] buffer = new byte[2048];
    int length = GetPrivateProfileString(sectionName,null,"", buffer, 999, filePath);
    String[] rs = System.Text.UTF8Encoding.Default.GetString(buffer, 0, length).Split(new string[] { "\0" }, StringSplitOptions.RemoveEmptyEntries);
    return rs.ToList();
}

 

3. WritePrivateProfileString()函數:寫入操做

C#能夠經過調用【kernel32.dll】文件中的 WritePrivateProfileString()函數對ini文件進行寫入操做。

官方APIhttps://msdn.microsoft.com/zh-cn/library/ms725501.aspx

函數簽名

[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string sectionName, string key, string value, string filePath);

成員

sectionName {string}:要寫入的區域名。

key {string | null}:key的名稱。若傳入null值,將移除指定的section。

value {string | null}:設置key所對應的值。若傳入null值,將移除指定的key。

filePath {string}:ini文件路徑。

支持的操做

1) 建立/設置key的值

2) 移除指定的section

3) 移除指定的key

 

3.1 建立/設置key的值

注意:若此key不存在將會建立,不然就爲修改此key的值。

/// <summary>
/// 保存內容到ini文件
/// <para>若存在相同的key,就覆蓋,不然就增長</para>
/// </summary>
/// <param name="sectionName">section名稱</param>
/// <param name="key">key的名稱</param>
/// <param name="value">存儲的值</param>
/// <param name="filePath">文件路徑</param>
public static bool SetValue(string sectionName, string key, string value, string filePath)
{
    int rs = (int)WritePrivateProfileString(sectionName, key, value, filePath);
    return rs > 0;
}

 

3.2 移除指定的section

說明:key參數傳入null就爲移除指定的section。

/// <summary>
/// 移除指定的section
/// </summary>
/// <param name="sectionName">section名稱</param>
/// <param name="filePath">文件路徑</param>
/// <returns></returns>
public static bool RemoveSection(string sectionName, string filePath)
{
    int rs = (int)WritePrivateProfileString(sectionName, null, "", filePath);
    return rs > 0;
}

  

3.3 移除指定的key

說明:value參數傳入null就爲移除指定的key。

/// <summary>
/// 移除指定的key
/// </summary>
/// <param name="sectionName">section名稱</param>
/// <param name="filePath">文件路徑</param>
/// <returns></returns>
public static bool Removekey(string sectionName, string key, string filePath)
{
    int rs = (int)WritePrivateProfileString(sectionName, key, null, filePath);
    return rs > 0;
}

 

4. 源碼下載

4.1 運行圖

 

4.2 下載地址

百度網盤http://pan.baidu.com/s/1dEQ3QuP

CSDNhttp://download.csdn.net/detail/polk6/9684148

 

相關文章
相關標籤/搜索