C 類庫 ini檔案操作類

2021-08-09 10:14:46 字數 3839 閱讀 5751

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.類庫原始碼

///

/// ini檔案操作類

///public

class inihelper

////// 寫ini檔案

//////

節///

鍵///

待寫入值

public

static

void

ini_write(string section, string key, string ivalue, string path)

////// 根據檔名建立檔案

//////

檔名稱以及路徑

public

static

void

ini_creat(string path)

}///

/// 刪除ini檔案中鍵

//////

節名稱///

鍵名稱///

ini檔案路徑

public

static

void

ini_del_key(string section,string key, string path)

////// 刪除ini檔案中節

//////

節名///

ini檔案路徑

public

static

void

ini_del_section(string section, string 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就行。

ini檔案操作類

using system using system.io using system.runtime.interopservices using system.text using system.collections using system.collections.specialized name...

Ini檔案操作類

using system using system.runtime.interopservices using system.text namespace win.cfg set public inifile string path windows api 對ini檔案寫方法 要在其中寫入新字串的小...

C 讀寫Ini檔案類

ini檔案就是擴充套件名為 ini 的檔案。在windows系統中,ini檔案是很多,最重要的就是 system.ini system32.ini 和 win.ini 該檔案主要存放使用者所做的選擇以及系統的各種引數。使用者可以通過修改ini檔案,來改變應用程式和系統的很多配置。但自從windows...