原創 INI檔案的讀寫操作

2022-02-16 20:33:35 字數 2425 閱讀 5029

ini

檔案的讀寫操作

在程式開發中,很多人會把引數儲存為

ini檔案格式,

ini檔案的乙個好處是可以儲存為乙個結構體主結構,如

[user]

name=test

userid=test

[server]

serverip=127.0.0.1

serverport=80

……很方便也很容易區分,而且不同節點下的名稱可以重複。讀取

name

時只需要讀取

user

節點下的

name

。ini

檔案的讀寫是需要呼叫

windows

的api

來操作的。該

api方法是在

kernel32.dll中。讀

ini檔案呼叫

private

static

extern

int getprivateprofilestring(string section, string key, string def, stringbuilder refval,int size, string filepath);

引數說明:

section

:ini檔案中的段落名稱;

key:ini檔案中的關鍵字;

def:無法讀取時候時候的預設數值;

retval

:讀取數值;

size

:數值的大小;

filepath

:ini檔案的完整路徑和名稱

寫ini

檔案呼叫

[dllimport("kernel32")]

private

static

extern

long writeprivateprofilestring(string section, string key, string val, string filepath);

引數說明:

section

:ini檔案中的段落;

key:ini檔案中的關鍵字;

val:ini檔案中關鍵字的數值;

filepath

:ini檔案的完整的路徑和名稱

具體**如下:

public

class

inifile

#endregion

#region

讀取配置檔案節

//////

讀取配置檔案節

///public

static

long

getprofilestring(

string

ssection, 

string

skey, 

refstring

refval, 

string

sfilename)

#endregion

#region

寫入ini檔案節

//////

寫入ini檔案節

///public

static

long

writeinifilestring(

string

ssection, 

string

skey, 

string

sval)

return

writeprivateprofilestring(ssection, skey, sval, sinifile);

}#endregion

#region

讀取ini檔案節

//////

讀取ini檔案節

///public

static

long

getinifilestring(

string

ssection, 

string

skey, 

refstring

refval)

#endregion

#region

讀取配置檔案

//////

讀取配置檔案

///public

static

bool

readinifile()

catch

(exception ex)

}#endregion

#region

寫入配置檔案

//////

寫入配置檔案

///public

static

bool

writeinifile()

catch

(exception ex)

}#endregion

}

INI檔案讀寫操作

檔案的位置 region 宣告讀寫ini檔案的api函式 引數說明 section ini 檔案中的段落 key ini檔案中的關鍵字 val ini檔案中關鍵字的數值 filepath ini 檔案的完整的路徑和名稱。dllimport kernel32 private static extern...

INI檔案讀寫

一 有必要了解ini檔案的結構 注釋 小節名 關鍵字 值 ini檔案允許有多個小節,每個小節又允許有多個關鍵字,後面是該關鍵字的值。值的型別有三種 字串 整型數值和布林值。其中字串存貯在ini檔案中時沒有引號,布林真值用1表示,布林假值用0表示。注釋以分號 開頭。二 定義 1 在inte ce的us...

讀寫ini檔案

using system using system.io using system.text using system.configuration using system.runtime.interopservices using system.collections.specialized us...