我的INI 配置文件讀寫動(dòng)態(tài)庫
工作需要,今天上午花時(shí)間看了一下INI 配置文件的相關(guān)文章,并添加到項(xiàng)目中。
后來想想,干脆封裝成DLL 動(dòng)態(tài)庫,并提供給大家使用,順便更新一下博客。^_^
INI 配置文件的格式
在早期的Windows 桌面系統(tǒng)中,主要是用INI 文件作為系統(tǒng)的配置文件,從Win95 以后開始轉(zhuǎn)向使用注冊(cè)表,但是還有很多系統(tǒng)配置是使用INI 文件的。其實(shí),INI 文件就是簡(jiǎn)單的text 文件,只不過這種txt 文件要遵循一定的INI 文件格式。
“.ini” 就是英文 “initialization” 的頭三個(gè)字母的縮寫;當(dāng)然INI file 的后綴名也不一定是".ini"也可以是".cfg",".conf ”或者是".txt"。
經(jīng)典格式:
INI文件的格式很簡(jiǎn)單,最基本的三個(gè)要素是:parameters,sections 和 comments。
什么是 parameters?
INI所包含的最基本的“元素”就是parameter;每一個(gè)parameter都有一個(gè)name和一個(gè)value,name和value是由等號(hào)“=”隔開。name在等號(hào)的左邊。
如: name = value
什么是sections ?
所有的parameters都是以sections為單位結(jié)合在一起的。所有的section名稱都是獨(dú)占一行,并且sections名字都被方括號(hào)包圍著 ([ and ])。在section聲明后的所有parameters都是屬于該section。對(duì)于一個(gè)section沒有明顯的結(jié)束標(biāo)志符,一個(gè)section的 開始就是上一個(gè)section的結(jié)束,或者是end of the file。Sections一般情況下不能被nested,當(dāng)然特殊情況下也可以實(shí)現(xiàn)sections的嵌套。
section 如: [section]
什么是 comments ?
在INI 文件中注釋語句是以分號(hào)“;”開始的。所有的注釋語句不管多長(zhǎng)都是獨(dú)占一行直到結(jié)束的。在分號(hào)和行結(jié)束符之間的所有內(nèi)容都是被忽略的。
注釋如: ;comments text
當(dāng)然,上面講的都是最經(jīng)典的INI文件格式,隨著使用的需求INI文件的格式也出現(xiàn)了很多變種;
變種格式:請(qǐng)參考:http://en.wikipedia.org/wiki/INI_file
我的 INI 配置文件讀寫動(dòng)態(tài)庫
其實(shí)就是調(diào)用了kernel32.dll 中的 WritePrivateProfileString 和 GetPrivateProfileString 函數(shù)。
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">節(jié)點(diǎn)名稱[如[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 節(jié)點(diǎn)名稱</param>
/// <param name="key">key 值</param>
/// <param name="value">value 值</param>
public void Write(string sectionName, string key, string value)
{
try
{
//根據(jù)INI文件名設(shè)置要寫入INI文件的節(jié)點(diǎn)名稱
//此處的節(jié)點(diǎn)名稱完全可以根據(jù)實(shí)際需要進(jìn)行配置
strFileName = Path.GetFileNameWithoutExtension(strFilePath);
INIFileHelper.WritePrivateProfileString(sectionName, key, value, strFilePath);
}
catch
{
throw new Exception("配置文件不存在或權(quán)限不足導(dǎo)致無法寫入");
}
}
/// <summary>
/// 寫入默認(rèn)節(jié)點(diǎn)"FileConfig"下的相關(guān)數(shù)據(jù)
/// </summary>
/// <param name="key">key 值</param>
/// <param name="value">value 值</param>
public void Write(string key, string value)
{
// section 節(jié)點(diǎn)名稱使用默認(rèn)值:"FileConfig"
Write("FileConfig", key, value);
}
/// <summary>
/// 讀取INI文件
/// </summary>
/// <param name="section">節(jié)點(diǎn)名稱</param>
/// <param name="key">鍵</param>
/// <param name="def">值</param>
/// <param name="retval">stringbulider對(duì)象</param>
/// <param name="size">字節(jié)大小</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 節(jié)點(diǎn)名稱</param>
/// <param name="key">key 值</param>
/// <returns>value 值</returns>
public string Read(string sectionName, string key)
{
if (File.Exists(strFilePath)) //讀取時(shí)先要判讀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>
/// 讀取默認(rèn)節(jié)點(diǎn)"FileConfig"下的相關(guān)數(shù)據(jù)
/// </summary>
/// <param name="key">key 值</param>
/// <returns>value 值</returns>
public string Read(string key)
{
// section 節(jié)點(diǎn)名稱使用默認(rèn)值:"FileConfig"
return Read("FileConfig", key);
}
}
}
對(duì) INIHelper.dll 動(dòng)態(tài)庫的使用和測(cè)試,代碼如下:
//寫入
private void button1_Click(object sender, EventArgs e)
{
//test1(WinForm 測(cè)試)
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 測(cè)試)
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"));
}
參考來源:
| 作者: XuGang 網(wǎng)名:鋼鋼 |
| 出處: http://xugang.cnblogs.com |
| 聲明: 本文版權(quán)歸作者和博客園共有。轉(zhuǎn)載時(shí)必須保留此段聲明,且在文章頁面明顯位置給出原文連接地址! |
posted on 2012-03-21 13:44 鋼鋼 閱讀(6041) 評(píng)論(4) 收藏 舉報(bào)
浙公網(wǎng)安備 33010602011771號(hào)