VB INI 读写类 代码

 
VERSION  1.0   CLASS
BEGIN
  MultiUse 
= -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name 
= "clsIniFile"
Attribute VB_GlobalNameSpace 
= False
Attribute VB_Creatable 
= False
Attribute VB_PredeclaredId 
= False
Attribute VB_Exposed 
= False
' Ini File Functions Class
'
 Copyright (C) 1996, Jens Balchen
'
'
 Uses
'
'
 Exposes
'
     Function GetSetting
'
     Function SaveSetting
'
     Function GetSection
'
'
 Comments

Option Explicit
'Powered by barenx
' --------
'
  Public
'
 --------
'
'
 Property for file to read
Public File As String

' ---------
'
  Private
'
 ---------
'
'
 API to read/write ini's
#If Win32 Then
   
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As StringByVal lpKeyName As Any, ByVal lpDefault As StringByVal lpReturnedString As StringByVal nSize As IntegerByVal lpFileName As StringAs Integer
   
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal Appname As StringByVal KeyName As Any, ByVal NewString As Any, ByVal Filename As StringAs Integer
#
Else
   
Private Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As StringByVal lpKeyName As Any, ByVal lpDefault As StringByVal lpReturnedString As StringByVal nSize As IntegerByVal lpFileName As StringAs Integer
   
Private Declare Function WritePrivateProfileString Lib "Kernel" (ByVal Appname As StringByVal KeyName As Any, ByVal NewString As Any, ByVal Filename As StringAs Integer
#
End If

Sub DeleteSection(ByVal Section As String)

Dim retval As Integer

   retval 
= WritePrivateProfileString(Section, 0&"", File)

End Sub

Public Function SaveSetting(ByVal Section$, ByVal Key$, ByVal Value$)

Dim retval As Integer

   
SaveSetting = WritePrivateProfileString(Section$, Key$, Value$, File)

End Function


Public Function GetSetting(ByVal Section As StringByVal KeyName As StringAs String

Dim retval As Integer
Dim t As String * 255

   
' Get the value
   retval = GetPrivateProfileString(Section, KeyName, "unknown value", t, Len(t), File)

   
' If there is one, return it
   If retval > 0 Then
      
GetSetting = Left$(t, retval)
   
Else
      
GetSetting = vbNullString
   
End If

End Function


Public Function GetSection(ByVal Section As String, KeyArray() As StringAs Integer

Dim retval As Integer
' Allocate space for return value
Dim t As String * 2500
Dim lastpointer As Integer
Dim nullpointer As Integer
Dim ArrayCount As Integer
Dim keystring As String
   
   
ReDim KeyArray(0)
   
   
' Get the value
   retval = GetPrivateProfileString(Section, 0&"", t, Len(t), File)
   
   
' If there is one, return it
   If retval > 0 Then
      
'
      ' Separate the keys and store them in the array
      nullpointer = InStr(t, Chr$(0))
      lastpointer 
= 1
      
Do While (nullpointer <> 0 And nullpointer > lastpointer + 1)
         
'
         ' Extract key string
         keystring = Mid$(t, lastpointer, nullpointer - lastpointer)
         
'
         ' Now add to array
         ArrayCount = ArrayCount + 1
         
ReDim Preserve KeyArray(ArrayCount)
         KeyArray(ArrayCount) 
= keystring
         
'
         ' Find next null
         lastpointer = nullpointer + 1
         nullpointer 
= InStr(nullpointer + 1, t, Chr$(0))
      
Loop
   
End If
   
'
   ' Return the number of array elements
   GetSection = ArrayCount
   
End Function

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值