VB操作读取ini文件代码详解

Option Explicit

    '定义API函数
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 WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long


    '函数说明
'-------------------------------------------------------------------------
' ReadKeyVal FileName, Section, Key
' ~~~~~~~~读取FileName文件中的段Section下的关键字Key的值KeyValue
' WriteKeyVal FileName, Section, Key, KeyValue
' ~~~~~~~~写入值KeyValue到FileName文件中的段Section下的关键字Key
'-------------------------------------------------------------------------
' DeleteSection FileName, Section
' ~~~~~~~~删除FileName文件中的段Section
' DeleteKey FileName, Section, Key
' ~~~~~~~~删除FileName文件中的段Section下的关键字Key
' DeleteKeyValue FileName, Section, Key
' ~~~~~~~~删除FileName文件中的段Section下的关键字Key的KeyValue
'-------------------------------------------------------------------------
' TotalSections FileName
' ~~~~~~~~统计FileName文件中的段Section的总数
' TotalKeys FileName
' ~~~~~~~~统计FileName文件中的关键字Key的总数
' NumKeys FileName, Section
' ~~~~~~~~统计FileName文件中的段Section下的关键字Key的总数
'-------------------------------------------------------------------------
' RenameSection FileName, Section, NewSectionName
' ~~~~~~~~使用值NewSectionName替换FileName文件中的段Section
' RenameKey FileName, Section, KeyName, NewKeyName
' ~~~~~~~~使用值NewKeyName替换FileName文件中的段Section下的关键字Key
'-------------------------------------------------------------------------
' GetKey FileName, Section, KeyIndexNum,
' ~~~~~~~~获得FileName文件中的段Section下的Key索引为KeyIndexNum的Key
' GetKey2 FileName, SectionIndexNum,KeyIndexNum
' ~~~~~~~~获得FileName文件中的段索引为Section下的Key索引为KeyIndexNum的Key
' GetSection FileName, SectionIndexNum
' ~~~~~~~~获得FileName文件中的段索引为Section下的Section
'-------------------------------------------------------------------------
' GetSectionIndex FileName, Section
' ~~~~~~~~获得FileName文件中的段Section的索引
' GetKeyIndex FileName, Section, Key
' ~~~~~~~~获得FileName文件中的段Section下的关键字Key的索引
' GetKeyIndex2 FileName, SectionIndexNum,Key
' ~~~~~~~~获得FileName文件中的段索引为Section下的关键字Key的索引
'-------------------------------------------------------------------------
' KeyExist FileName, Section, Key
' ~~~~~~~~检测是FileName文件中的段Section下的Key是否存在
' KeyExist2 FileName, SectionIndexNum,Key
' ~~~~~~~~检测是FileName文件中的段索引为Section下的Key是否存在
' SectionExist FileName, Section
' ~~~~~~~~检测是FileName文件中的段Section是否存在
'-------------------------------------------------------------------------
' IsKey TextLine
' ~~~~~~~~检测是TextLine是否是Key行
' IsSection TextLine
' ~~~~~~~~检测是TextLine是否是Section行
'-------------------------------------------------------------------------


    'ini文件示例
'-------------------------------------------------------------------------
'[Section 1]
'Key 1=Key1Value
'[Section 2]
'Key 1=Key1Value
'Key 2=Key2Value
'Key 3=Key3Value
'Key 4=Key4Value
'Key 5=Key5Value
'[Section 3]
'Key 1=Key1Value
'Key 2=Key2Value
'Key 3=Key3Value

'-------------------------------------------------------------------------

    '正文
Private Function ReadKeyVal(ByVal FileName As String, ByVal Section As String, ByVal Key As String) As String
  Dim RetVal As String, Worked As Integer
  ReadKeyVal = ""
  If Dir(FileName) = "" Then MsgBox FileName & " not found.", vbCritical, "File Not Found": Exit Function
  RetVal = String$(255, Chr(0))
  Worked = GetPrivateProfileString(Section, Key, "", RetVal, Len(RetVal), FileName)
  ReadKeyVal = IIf(Worked = 0, "", Left(RetVal, InStr(RetVal, Chr(0)) - 1))
End Function

'---------------------------
Private Function WriteKeyVal(ByVal FileName As String, ByVal Section As String, ByVal Key As String, ByVal KeyValue As String) As Long
  WriteKeyVal = 0
  If Dir(FileName) = "" Then MsgBox FileName & " not found.", vbCritical, "File Not Found": Exit Function
  WriteKeyVal = WritePrivateProfileString(Section, Key, KeyValue, FileName)
End Function

'---------------------------
Private Function DeleteSection(ByVal FileName As String, ByVal Section As String) As Long
  DeleteSection = 0
  If Dir(FileName) = "" Then MsgBox FileName & " not found.", vbCritical, "File Not Found": Exit Function
  If Not SectionExists(FileName, Section) Then MsgBox "Section, " & Section & ", Not Found. ~(DeleteSection)" & vbCrLf & "Verify spelling and capitilization is correct. Case-sensative.", vbInformation, "Section Not Found.": Exit Function
  DeleteSection = WritePrivateProfileString(Section, vbNullString, vbNullString, FileName)
End Function

'---------------------------
Private Function DeleteKey(ByVal FileName As String, ByVal Section As String, ByVal Key As String) As Long
  DeleteKey = 0
  If Dir(FileName) = "" Then MsgBox FileName & " not found.", vbCritical, "File Not Found": Exit Function
  If Not SectionExists(FileName, Section) Then MsgBox "Section, " & Section & ", Not Found. ~(DeleteKey)" & vbCrLf & "Verify spelling and capitilization is correct. Case-sensative.", vbInformation, "Section Not Found.": Exit Function
  If Not KeyExists(FileName, Section, Key) Then MsgBox "Key, " & Key & ", Not Found. ~(DeleteKey)" & vbCrLf & "Verify spelling and capitilization is correct. Case-sensative.", vbInformation, "Key Not Found.": Exit Function
  DeleteKey = WritePrivateProfileString(Section, Key, vbNullString, FileName)
End Function

'---------------------------
Private Function DeleteKeyValue(ByVal FileName As String, ByVal Section As String, ByVal Key As String) As Integer
  DeleteKeyValue = 0
  If Dir(FileName) = "" Then MsgBox FileName & " not found.", vbCritical, "File Not Found": Exit Function
  If Not SectionExists(FileName, Section) Then MsgBox "Section, " & Section & ", Not Found. ~(DeleteKeyValue)" & vbCrLf & "Verify spelling and capitilization is correct. Case-sensative.", vbInformation, "Section Not Found.": Exit Function
  If Not KeyExists(FileName, Section, Key) Then MsgBox "Key, " & Key & ", Not Found. ~(DeleteKeyValue)" & vbCrLf & "Verify spelling and capitilization is correct. Case-sensative.", vbInformation, "Key Not Found.": Exit Function
  DeleteKeyValue = WritePrivateProfileString(Section, Key, "", FileName)
End Function

来源:PC诊所
地址:http://www.pczs120.com/article/art349.htm
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值