我的INI 配置文件讀寫動態庫


 

工作需要,今天上午花時間看了一下INI 配置文件的相關文章,並添加到項目中。

后來想想,干脆封裝成DLL 動態庫,並提供給大家使用,順便更新一下博客。^_^

 

INI 配置文件的格式   

在早期的Windows 桌面系統中,主要是用INI 文件作為系統的配置文件,從Win95 以后開始轉向使用注冊表,但是還有很多系統配置是使用INI 文件的。其實,INI 文件就是簡單的text 文件,只不過這種txt 文件要遵循一定的INI 文件格式。

“.ini” 就是英文 “initialization” 的頭三個字母的縮寫;當然INI file 的后綴名也不一定是".ini"也可以是".cfg",".conf ”或者是".txt"。

經典格式:

INI文件的格式很簡單,最基本的三個要素是:parameters,sections 和 comments。

什么是 parameters?

INI所包含的最基本的“元素”就是parameter;每一個parameter都有一個name和一個value,name和value是由等號“=”隔開。name在等號的左邊。

如:  name = value

什么是sections ?

所有的parameters都是以sections為單位結合在一起的。所有的section名稱都是獨占一行,並且sections名字都被方括號包圍着 ([ and ])。在section聲明后的所有parameters都是屬於該section。對於一個section沒有明顯的結束標志符,一個section的 開始就是上一個section的結束,或者是end of the file。Sections一般情況下不能被nested,當然特殊情況下也可以實現sections的嵌套。

section 如:   [section]

 什么是 comments ?

在INI 文件中注釋語句是以分號“;”開始的。所有的注釋語句不管多長都是獨占一行直到結束的。在分號和行結束符之間的所有內容都是被忽略的。

注釋如:   ;comments text

 

當然,上面講的都是最經典的INI文件格式,隨着使用的需求INI文件的格式也出現了很多變種;

變種格式:請參考:http://en.wikipedia.org/wiki/INI_file

 

我的 INI 配置文件讀寫動態庫

其實就是調用了kernel32.dll 中的 WritePrivateProfileString 和 GetPrivateProfileString 函數。

kernel32.dll是Windows 9x/Me 中非常重要的32位動態鏈接庫文件,屬於內核級文件。它控制着系統的內存管理、數據的輸入輸出操作和中斷處理。
 
INI 配置文件讀寫動態庫 INIHelper.dll 的源碼很簡單,代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace INIHelper
{
public class INIFileHelper
{
private string strFileName = ""; //INI文件名
private string strFilePath = "";//獲取INI文件路徑

public INIFileHelper()
{
strFileName = "Config.ini"; //INI文件名
//方法1獲取INI文件路徑
strFilePath = Directory.GetCurrentDirectory() + "\\" + strFileName;
//方法2:獲取INI文件路徑
//strFilePath = Path.GetFullPath(".\\") + strFileName;
}

public INIFileHelper(string FileName)
{
strFileName = FileName; //INI文件名
//
獲取INI文件路徑
strFilePath = Directory.GetCurrentDirectory() + "\\" + strFileName;
}

public INIFileHelper(string FullPath, string FileName)
{
strFileName = FileName; //INI文件名
strFilePath = FullPath + "\\" + strFileName;//獲取INI文件路徑
}

/// <summary>
/// 寫入INI文件
/// </summary>
/// <param name="section">節點名稱[如[TypeName]]</param>
/// <param name="key"></param>
/// <param name="val"></param>
/// <param name="filepath">文件路徑</param>
/// <returns></returns>
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string val, string filepath);

/// <summary>
/// 寫入
/// </summary>
/// <param name="sectionName">section 節點名稱</param>
/// <param name="key">key 值</param>
/// <param name="value">value 值</param>
public void Write(string sectionName, string key, string value)
{
try
{
//根據INI文件名設置要寫入INI文件的節點名稱
//此處的節點名稱完全可以根據實際需要進行配置
strFileName = Path.GetFileNameWithoutExtension(strFilePath);
INIFileHelper.WritePrivateProfileString(sectionName, key, value, strFilePath);
}
catch
{
throw new Exception("配置文件不存在或權限不足導致無法寫入");
}
}

/// <summary>
/// 寫入默認節點"FileConfig"下的相關數據
/// </summary>
/// <param name="key">key 值</param>
/// <param name="value">value 值</param>
public void Write(string key, string value)
{
// section 節點名稱使用默認值:"FileConfig"
Write("FileConfig", key, value);
}

/// <summary>
/// 讀取INI文件
/// </summary>
/// <param name="section">節點名稱</param>
/// <param name="key"></param>
/// <param name="def"></param>
/// <param name="retval">stringbulider對象</param>
/// <param name="size">字節大小</param>
/// <param name="filePath">文件路徑</param>
/// <returns></returns>
[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);

/// <summary>
/// 讀取
/// </summary>
/// <param name="sectionName">section 節點名稱</param>
/// <param name="key">key 值</param>
/// <returns>value 值</returns>
public string Read(string sectionName, string key)
{
if (File.Exists(strFilePath)) //讀取時先要判讀INI文件是否存在
{
strFileName = Path.GetFileNameWithoutExtension(strFilePath);
//return ContentValue(strFileName, key);
StringBuilder outValue = new StringBuilder(1024);
INIFileHelper.GetPrivateProfileString(sectionName, key, "", outValue, 1024, strFilePath);
return outValue.ToString();
}
else
{
throw new Exception("配置文件不存在");
}
}

/// <summary>
/// 讀取默認節點"FileConfig"下的相關數據
/// </summary>
/// <param name="key">key 值</param>
/// <returns>value 值</returns>
public string Read(string key)
{
// section 節點名稱使用默認值:"FileConfig"
return Read("FileConfig", key);
}

}
}
 

對 INIHelper.dll 動態庫的使用和測試,代碼如下:

// 獲取INI文件路徑
//  private string strFilePath = Application.StartupPath + "\\FileConfig.ini";

// 寫入
private  void button1_Click( object sender, EventArgs e)
{
     // test1(WinForm 測試)
     string strFilePath =  " Config.ini "; // 獲取INI文件路徑
    INIFileHelper file1 =  new INIFileHelper(strFilePath);
    file1.Write(label1.Text, textBox1.Text);
    file1.Write(label2.Text, textBox2.Text);
    MessageBox.Show( " test1 寫入完畢 ");

     // test2
    INIFileHelper file2 =  new INIFileHelper();
    file2.Write( " xugang "" http://xugang.cnblogs.com ");
    file2.Write( " hobby "" @#$%^&*() ");
    MessageBox.Show( " test2 寫入完畢 ");

     // test3
    INIFileHelper file3 =  new INIFileHelper( " newConfig.ini ");
    file3.Write( " NewSection "" xugang "" http://xugang.cnblogs.com ");
    file3.Write( " NewSection "" hobby "" @#$%^&*() ");
    MessageBox.Show( " test3 寫入完畢 ");

     // test4
     string strPath = Application.StartupPath;  // 文件路徑
     string strName =  " xxx.ini "; // INI文件名稱

    INIFileHelper file4 =  new INIFileHelper(strPath, strName);
    file4.Write( " NewSection "" xugang "" http://xugang.cnblogs.com ");
    file4.Write( " NewSection "" hobby "" @#$%^&*() ");
    MessageBox.Show( " test4 寫入完畢 ");
}

// 讀取
private  void button2_Click( object sender, EventArgs e)
{
     // test1(WinForm 測試)
     string strFilePath =  " Config.ini "; // 獲取INI文件路徑
    INIFileHelper file1 =  new INIFileHelper(strFilePath);
    StringBuilder str =  new StringBuilder();
    str.AppendLine(file1.Read(label1.Text));
    str.AppendLine(file1.Read(label2.Text));
    MessageBox.Show(str.ToString());

     // test2
    INIFileHelper file2 =  new INIFileHelper();
    MessageBox.Show(file2.Read( " xugang ") + "     "+file2.Read( " hobby "));

     // test3
    INIFileHelper file3 =  new INIFileHelper( " newConfig.ini ");
    MessageBox.Show( file3.Read( " NewSection "" xugang ")
           +  "     " + file3.Read( " NewSection "" hobby "));

     // test4
     string strPath = Application.StartupPath;  // 文件路徑
     string strName =  " xxx.ini "; // INI文件名稱

    INIFileHelper file4 =  new INIFileHelper(strPath, strName);
    MessageBox.Show(file4.Read( " NewSection "" xugang ")
          +  "     " + file4.Read( " NewSection "" hobby "));
}
 

參考來源:

INI 配置文件的格式

INI 格式文件操作

 

源碼下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM