Attribute VB_Name = "mIni"'*************************************************************************'**模 塊 名:mIni'**說(shuō) 明:孤帆 版權所有2005 - 2006(C)'**創(chuàng ) 建 人:孤帆'**日 期:2005-5-25 13:16:33'**描 述:讀寫(xiě)ini文件鍵值/段值模塊(可以窮舉一個(gè)ini文件里的所有段名'** 和指定段的鍵名/鍵值)'**版 本:V1.0.0'*************************************************************************Option Base 0Private Declare Function GetPrivateProfileIntA Lib "kernel32" (ByVal Senction$, ByVal lpKeyName$, ByVal nDefault&, ByVal lpFileName$) As LongPrivate Declare Function GetPrivateProfileSectionNamesA Lib "kernel32.dll" (ByVal szValue$, ByVal nSize&, ByVal szFileName$) As LongPrivate Declare Function WritePrivateProfileSectionA Lib "kernel32" (ByVal Senction$, ByVal szValue$, ByVal szFileName$) As LongPrivate Declare Function GetPrivateProfileSectionA Lib "kernel32" (ByVal Senction$, ByVal szValue As String, ByVal nSize&, ByVal szFileName$) As LongPrivate Declare Function WritePrivateProfileStringA Lib "kernel32" (ByVal Section$, ByVal Key$, ByVal szValue$, ByVal lpFileName$) As LongPrivate Declare Function GetPrivateProfileStringA Lib "kernel32" (ByVal Senction$, ByVal Key As Any, ByVal lpDefault$, _ ByVal szValue$, ByVal nSize As Long, ByVal szFileName$) As LongPrivate m_Path$'--------------------------------' 一個(gè)ini段中的數據結構' 通過(guò)次結構窮舉指定段里的' 鍵的數據'--------------------------------Public Type TSection kName As String '鍵名 kValue As String '鍵值End Type'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<' 屬性'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<'*************************************************************************' ini文件路徑屬性'*************************************************************************Public Property Let Path(ByVal szValue$) m_Path = szValueEnd PropertyPublic Property Get Path() As String Path = m_PathEnd Property'*************************************************************************' 獲取當前程序文件路徑(后加"/")'*************************************************************************Property Get AppPath() As String AppPath = App.Path If Right$(AppPath, 1) <> "/" Then AppPath = AppPath & "/"End Property'*************************************************************************' 鎖定ini文件屬性' 參 數:是否鎖定'*************************************************************************Property Let Locked(ByVal bYes As Boolean) On Error GoTo Out If bYes Then Call SetAttr(m_Path, vbNormal) Else Call SetAttr(m_Path, vbHidden Or vbReadOnly Or vbSystem) End IfOut:End Property'*************************************************************************' 獲取ini文件大小屬性'*************************************************************************Property Get iniSize() As Long iniSize = FileLen(m_Path)End Property'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<' 讀寫(xiě)ini鍵值'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<'*************************************************************************' 讀取ini段里的字符串鍵值' 參 數:段名,鍵名,鍵值' 返回值:鍵值'*************************************************************************Function getStrKey(ByVal Section$, ByVal KeyName$, Optional ByVal szDefaultValue$ = vbNullString) As String Dim szBuffer$, nLen% szBuffer = String$(1024, 0) nLen = GetPrivateProfileStringA(Section, KeyName, szDefaultValue, szBuffer, 1024, m_Path) If nLen > 0 Then getStrKey = Left$(szBuffer, nLen)End Function'*************************************************************************' 寫(xiě)ini段里的字符串鍵值' 參 數:段名,鍵名,鍵值' 返回值:成功則為true'*************************************************************************Function setStrKey(ByVal Section$, ByVal KeyName$, Optional ByVal szValue$ = vbNullString) As Boolean setStrKey = WritePrivateProfileStringA(Section, KeyName, szValue, m_Path)End Function'*************************************************************************' 讀取ini段里的整形鍵值' 參 數:段名,鍵名,鍵值' 返回值:鍵值'*************************************************************************Function getIntKey(ByVal Section$, ByVal KeyName$, Optional DefaultValue& = -1) As Long getIntKey = GetPrivateProfileIntA(Section, KeyName, DefaultValue, m_Path)End Function'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<' 讀寫(xiě)ini段值'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<'*************************************************************************' 讀取ini段里的所有字符串到一個(gè)TSection結構的數組里' 參 數:段名,提供返回段中字符串的動(dòng)態(tài)TSection數組' 返回值:成功則返回數組下限,否則返回-1'*************************************************************************Function getStrSection2Structs(ByVal Section$, rSection() As TSection) As Long Dim strTmp() As String, strTmp2() As String, szBuffer$ Dim nLen%, I%, Bottom% szBuffer = String$(32767, 0) nLen = GetPrivateProfileSectionA(Section, szBuffer, 32767, m_Path) If nLen > 0 Then On Error GoTo Out szBuffer = Left$(szBuffer, nLen) Tmp2 = Split(szBuffer, vbNullChar, nLen) '分解出每一個(gè)鍵的數據 Bottom = UBound(Tmp2) - 1 ReDim rSection(Bottom) For I = 0 To Bottom Tmp = Split(Tmp2(I), "=") '分解鍵名和鍵值 rSection(I).kName = Tmp(0) rSection(I).kValue = Tmp(1) Next getStrSection2Structs = Bottom Else getStrSection2Structs = -1 End IfOut:End Function'*************************************************************************' 讀取ini段里的所有字符串' 參 數:段名,提供返回段中字符串的動(dòng)態(tài)字符串數組(每一行一個(gè)元素)' 返回值:成功則返回數組下限,否則返回-1'*************************************************************************Function getStrSection(ByVal Section$, rValue() As String) As Long Dim szBuffer$ Dim nLen%, I%, Bottom% szBuffer = String$(32767, 0) nLen = GetPrivateProfileSectionA(Section, szBuffer, 32767, m_Path) If nLen > 0 Then On Error GoTo Out szBuffer = Left$(szBuffer, nLen) rValue = Split(szBuffer, vbNullChar, nLen) Bottom = UBound(rValue) - 1 getStrSection = Bottom Else getStrSection = -1 End IfOut:End Function'*************************************************************************' 寫(xiě)一個(gè)ini段' 參 數:段名,段值(缺省為刪除這個(gè)段,鍵與鍵之間的數據以vbNullChar分隔且以vbNullChar結尾)' 返回值:成功則為true'*************************************************************************Function setStrSection(ByVal Section$, Optional ByVal szValue$ = vbNullString) As Boolean setStrSection = WritePrivateProfileSectionA(Section, szValue, m_Path)End Function'*************************************************************************' 讀取ini文件里的所有段名' 參 數:提供返回段名的動(dòng)態(tài)字符串數組' 返回值:成功則返回數組下限,否則返回-1'*************************************************************************Function getSectionsName(rSectionName() As String) As Long Dim szBuffer$, nLen% szBuffer = String(1024, 0) nLen = GetPrivateProfileSectionNamesA(szBuffer, 1024, m_Path) If nLen = 0 Then getSectionsName = -1 Exit Function End If If nLen > 0 Then szBuffer = Left$(szBuffer, nLen) On Error GoTo Out rSectionName = Split(szBuffer, vbNullChar, nLen) getSectionsName = UBound(rSectionName) - 1 End IfOut:End Function
聯(lián)系客服