明晨网络

电话: 136-6532-7492 QQ: 给我发送消息 8507-0741

VB下的INI配置文件读写类模块

网络,2009-06-12 18:03, 文章标签: 类模块 VB6 INI
代码如下
  1. '========================================
  2. '模块名称:cls_Ini
  3. '函数作用:读写INI文件,明晨网络mingchennet.com收集整理
  4. '========================================
  5. Option Explicit
  6. Private iniFileName As String
  7. Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
  8. 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
  9. 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
  10.  
  11. '========================================
  12. '函数名称:GetIniS
  13. '函数作用:从INI配置文件中读取指定段的字符串
  14. '========================================
  15. Public Function GetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefString As String) As String
  16.     Const ProStringLen = 8192
  17.     Dim Res      As Long, S        As String, i        As Long
  18.     S = Space(ProStringLen)
  19.     Res = GetPrivateProfileString(SectionName, KeyWord, DefString, S, ProStringLen, iniFileName)
  20.     S = Trim(Left(S, Res))
  21.     If Len(S) > 0 Then
  22.        
     
  23.         i = Len(S)
  24.             Do While Mid(S, i, 1) = vbNullChar
  25.                 i = i - 1
  26.             Loop
  27.         S = Left(S, i)
  28.     Else
  29.         SetIniS SectionName, KeyWord, DefString
  30.         S = DefString
  31.     End If
  32.     GetIniS = Trim(S)
  33. End Function
  34.  
  35. '========================================
  36. '函数名称:GetIniN
  37. '函数作用:从INI配置文件中读取指定段的数值
  38. '========================================
  39. Public Function GetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefValue As Long) As Integer
  40.     Dim d As Long, S As String
  41.     d = DefValue
  42.     GetIniN = GetPrivateProfileInt(SectionName, KeyWord, DefValue, iniFileName)
  43.     If d <> DefValue Then
  44.     S = "" & d
  45.     d = WritePrivateProfileString(SectionName, KeyWord, S, iniFileName)
  46.     End If
  47. End Function
  48.  
  49. '========================================
  50. '函数名称:SetIniS
  51. '函数作用:将字符串写入指定段
  52. '========================================
  53. Public Sub SetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String)
  54.     Dim Res%
  55.     Res% = WritePrivateProfileString(SectionName, KeyWord, ValStr, iniFileName)
  56. End Sub
  57.  
  58. '========================================
  59. '函数名称:SetIniN
  60. '函数作用:将数值写入指定段
  61. '========================================
  62. Public Sub SetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValInt As Long)
  63.     Dim Res%, S$
  64.     S$ = Str$(ValInt)
  65.     Res% = WritePrivateProfileString(SectionName, KeyWord, S$, iniFileName)
  66. End Sub
  67.  
  68.  
  69. '========================================
  70. '函数名称:DelIniKey
  71. '函数作用:清除指定段的值
  72. '========================================
  73. Public Sub DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)
  74.     Dim RetVal As Integer
  75.     RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, iniFileName)
  76. End Sub
  77.  
  78.  
  79. '========================================
  80. '函数名称:DelIniSec
  81. '函数作用:清除指定段
  82. '========================================
  83. Public Sub DelIniSec(ByVal SectionName As String)
  84.     Dim RetVal As Integer
  85.     RetVal = WritePrivateProfileString(SectionName, 0&, "", iniFileName)
  86. End Sub
  87.  
  88. '========================================
  89. '属性名称:Path
  90. '属性作用:INI文件的路径
  91. '========================================
  92. Public Property Get Path() As String
  93.     Path = iniFileName
  94. End Property
  95. Public Property Let Path(ByVal sPath As String)
  96.     iniFileName = sPath
  97. End Property
  98.  
  99.  
  100.  
  101.  
  102.  

文章源自:明晨网络,网络,《VB下的INI配置文件读写类模块》,http://www.mingchennet.com/tec/code/vb/20.htm