雖然vb.net中讀取xml配置信息很方便,但有時開發的過程中還是要用到ini文件,在vb.net中讀取ini卻不像vb中那么方便了,剛才寫了個函數,現貼出來,也許各位能用得上。
'函數名: sdgetiniinfo
'功能:讀取ini文件設置信息
'參數說明:inifile-->ini文件 inisection--ini文件中設置的部分名稱
'作者:sd
'日期:2005-10-11
'email:[email protected]
'備注:轉載或修改請保留此信息
function sdgetiniinfo(byval inifile as string, byval inisection as string) as string
if not file.exists(inifile) then
return "文件 " & inifile & " 未找到,請確認路徑和文件名是否正確!"
exit function
end if
dim iniread as new streamreader(inifile)
dim inistr as string = iniread.readtoend
dim i as integer
dim cline as integer
dim nosec as boolean = false
dim getvalue as string = ""
dim clst
clst = inistr.split(chr(13))
cline = ubound(clst)
for i = 0 to cline
if clst(i).indexof("=") > 0 then
if clst(i).split("=")(0).trim() = inisection then
nosec = true
getvalue = clst(i).split("=")(1).trim()
exit for
end if
end if
next
if nosec = true then
return getvalue
else
return "沒有找到 " & inisection & " 的設置信息!"
end if
end function
說明:在引用的面頁中要先引用 imports system.io
eg:
set.ini文件內容:
[info]
name=zhuang
age=20
private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
dim name as string
name = sdgetiniinfo(application.startuppath & "/set.ini", "name")
msgbox(name)
end sub
新聞熱點
疑難解答
圖片精選