C 對ini配置檔案的讀寫

2021-06-23 07:08:06 字數 4185 閱讀 3905

[csharp]view plain

copy

public

class

win32api  

, stringsplitoptions.removeemptyentries);  

}  //釋放記憶體

marshal.freecotaskmem(preturnedstring);  

return

sections;  

}  /// 

/// 獲取ini檔案中指定節點(section)中的所有條目(key=value形式)

/// 

/// ini檔案

/// 節點名稱

/// 指定節點中的所有專案,沒有內容返回string[0]

public

static

string

inigetallitems(

string

inifile, 

string

section)  

, stringsplitoptions.removeemptyentries);  

}  marshal.freecotaskmem(preturnedstring);     //釋放記憶體

return

items;  

}  /// 

/// 獲取ini檔案中指定節點(section)中的所有條目的key列表

/// 

/// ini檔案

/// 節點名稱

/// 如果沒有內容,反回string[0]

public

static

string

inigetallitemkeys(

string

inifile, 

string

section)  

char

chars = 

newchar

[size];  

uint

bytesreturned = win32api.getprivateprofilestring(section, 

null

, null

, chars, size, inifile);  

if(bytesreturned != 0)  

, stringsplitoptions.removeemptyentries);  

}  chars = null

;  return

value;  

}  /// 

/// 讀取ini檔案中指定key的字串型值

/// 

/// ini檔案

/// 節點名稱

/// 鍵名稱

/// 如果沒此key所使用的預設值

/// 讀取到的值

public

static

string

inigetstringvalue(

string

inifile, 

string

section, 

string

key, 

string

defaultvalue)  

if(string

.isnullorempty(key))  

stringbuilder sb = new

stringbuilder(size);  

uint

bytesreturned = win32api.getprivateprofilestring(section, key, defaultvalue, sb, size, inifile);  

if(bytesreturned != 0)  

sb = null

;  return

value;  

}  /// 

/// 在ini檔案中,將指定的鍵值對寫到指定的節點,如果已經存在則替換

/// 

/// ini檔案

/// 節點,如果不存在此節點,則建立此節點

/// 鍵值對,多個用\0分隔,形如key1=value1\0key2=value2

/// 

public

static

bool

iniwriteitems(

string

inifile, 

string

section, 

string

items)  

if(string

.isnullorempty(items))  

return

win32api.writeprivateprofilesection(section, items, inifile);  

}  /// 

/// 在ini檔案中,指定節點寫入指定的鍵及值。如果已經存在,則替換。如果沒有則建立。

/// 

/// ini檔案

/// 節點

/// 鍵

/// 值

/// 操作是否成功

public

static

bool

iniwritevalue(

string

inifile, 

string

section, 

string

key, 

string

value)  

if(string

.isnullorempty(key))  

if(value == 

null

)    

return

win32api.writeprivateprofilestring(section, key, value, inifile);  

}  /// 

/// 在ini檔案中,刪除指定節點中的指定的鍵。

/// 

/// ini檔案

/// 節點

/// 鍵

/// 操作是否成功

public

static

bool

inideletekey(

string

inifile, 

string

section, 

string

key)  

if(string

.isnullorempty(key))  

return

win32api.writeprivateprofilestring(section, key, 

null

, inifile);  

}  /// 

/// 在ini檔案中,刪除指定的節點。

/// 

/// ini檔案

/// 節點

/// 操作是否成功

public

static

bool

inideletesection(

string

inifile, 

string

section)  

return

win32api.writeprivateprofilestring(section, 

null

, null

, inifile);  

}  /// 

/// 在ini檔案中,刪除指定節點中的所有內容。

/// 

/// ini檔案

/// 節點

/// 操作是否成功

public

static

bool

iniemptysection(

string

inifile, 

string

section)  

return

win32api.writeprivateprofilesection(section, 

string

.empty,  inifile);  

}  #endregion

#endregion

}  

測試**

[csharp]view plain

copy

private

void

test()  

C 讀寫ini配置檔案

配置檔案中經常用到ini檔案,在vc中其函式分別為 寫入.ini檔案 bool writeprivateprofilestring lpctstr lpstring,鍵值,也就是資料 lpctstr lpfilename ini檔案的路徑 讀取.ini檔案 dword getprivateprofi...

讀寫配置檔案 ini

配置檔案中經常用到ini檔案,在vc中其函式分別為 其中個引數的意思 lpctstr lpstring 是鍵值,也就是變數的值,必須為lpctstr或cstring型別 lpctstr lpfilename 完整的ini檔案路徑名 lpctstr lpdefaut 如果沒有其前兩個引數值,則將此值賦...

讀寫配置檔案 ini

在我們寫的程式當中,總有一些配置資訊需要儲存下來,以便完成程式的功能,最簡單的辦法就是將這些資訊寫入ini檔案中,程式初始化時再讀入.具體應用如下 一.將資訊寫入.ini檔案中.1.所用的winapi函式原型為 bool writeprivateprofilestring lpctstr lpkey...