Discuz!NT
欢迎 游客 , 注册 | 登录 | 会员 | 界面 | 简洁版本 | 在线 | 帮助
商都网教育宝典宝库

发表新主题 回复该主题
本主题被查看359次, 共1个帖子, 1页, 当前为第1页     选择页数: 1      跳转到第   上一主题   下一主题
标题: VB源码推荐:一个操作Ini文件的类
-[尕硴]
超级版主
UID: 71
来自:
精华: 130
积分: 14003
帖子: 12909
注册: 2007-10-22 17:59:00
状态: 离线
威望: 444.00
金钱: 3355.00 元
只看楼主 2008-01-28 14:50
VB源码推荐:一个操作Ini文件的类
【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)
#1  
发表新主题 回复该主题
本主题被查看359次, 共1个帖子, 1页, 当前为第1页     选择页数: 1      跳转到第







现在的时间是 2008-10-07 15:55:05

版权所有 商都网教育宝典
         Powered by Discuz!NT 1.0.6666    Copyright © 2001-2008 Comsenz Inc.
Processed in 0 seconds