VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 一个操作ini文件的类
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:10814, 日期:2000-12-30
【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)