【vb源码推荐:一个操作ini文件的类】
'--------cinifile.cls 代码----------------
'这里定义了一个cinifile类
option explicit
'// private member that holds a reference to
'// the path of our ini file
private strini as string
'// win api declares
private declare function writeprivateprofilestring _
lib "kernel32" alias "writeprivateprofilestringa" _
(byval lpapplicationname as string, _
byval lpkeyname as any, _
byval lpstring as any, _
byval lpfilename as string) as long
private declare function getprivateprofilestring _
lib "kernel32" alias "getprivateprofilestringa" _
(byval lpapplicationname as string, _
byval lpkeyname as any, _
byval lpdefault as string, _
byval lpreturnedstring as string, _
byval nsize as long, _
byval lpfilename as string) as long
private function makepath(byval strdrv as string, _
byval strdir as string) as string
'// makes an ini file: guarantees a sub dir
do while right$(strdrv, 1) = "\"
strdrv = left$(strdrv, len(strdrv) - 1)
loop
do while left$(strdir, 1) = "\"
strdir = mid$(strdir, 2)
loop
'// return the path
makepath = strdrv & "\" & strdir
end function
public sub createini(strdrv as string, strdir as string)
'// make a new ini file
strini = makepath(strdrv, strdir)
end sub
public sub writefile(strsection as string, _
strkey as string, _
strvalue as string)
'// write to strini
writeprivateprofilestring strsection, _
strkey, strvalue, strini
end sub
public function getfile(strsection as string, _
strkey as string) as string
dim strtmp as string
dim lngret as string
strtmp = string$(100, chr(32))
lngret = getprivateprofilestring(strsection, _
strkey, "", strtmp, _
len(strtmp), strini)
getfile = strtmp
end function
public property let inifile(byval new_inipath as string)
'// sets the new ini path
strini = new_inipath
end property
public property get inifile() as string
'// returns the current ini path
inifile = strini
end property
'--------cinifile.cls 使用举例----------------
dim myinifile as new cinifile
'---指定访问的ini文件
if len(app.path) > 3 then
'under disk root dir , eg: "c:\"
myinifile.inifile = app.path & "\setting.ini"
else
myinifile.inifile = app.path & "setting.ini"
end if
'---写入ini文件
myinifile.writefile "setting", "username", struser
'---读出ini文件的数据
' 注意,如果是字符串,则去掉末尾一个字符
' ----flybird@chinaasp.com
struser = trim(myinifile.getfile("setting", "username"))
struser = left(struser, len(struser) - 1)