1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
Option Explicit
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 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 GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
'Declare Function GetProfileSection Lib "kernel32" Alias "GetProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
'Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
'Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long
'Declare Function WriteProfileSection Lib "kernel32" Alias "WriteProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String) As Long
Public Sub SaveSetting(AppName As String, Section As String, Key As String, Setting As String)
Dim lRet As Long
Dim Path As String
Path = App.Path
If Right(Path, 1) <> "\" Then
Path = Path & "\"
End If
lRet = WritePrivateProfileString(Section, Key, Setting, Path & AppName & ".ini")
End Sub
Public Function GetSetting(AppName As String, Section As String, Key As String, Optional Default As String) As String
Dim lRet As Long
Dim Path As String
Dim strTemp As String
strTemp = Space(32567)
Path = App.Path
If Right(Path, 1) <> "\" Then
Path = Path & "\"
End If
lRet = GetPrivateProfileString(Section, Key, Default, strTemp, Len(strTemp), Path & AppName & ".ini")
lRet = InStr(strTemp, Chr$(0))
If lRet = 0 Then
GetSetting = ""
Else
GetSetting = Left(strTemp, lRet - 1)
End If
End Function
Public Function GetAllSettings(AppName As String, Section As String) As Variant
Dim lRet As Long
Dim Path As String
Dim strTemp As String
Dim Table() As String
Dim Table2() As String
Dim iPnt As Integer
Dim iPnt2 As Integer
Dim iposit As Integer
strTemp = Space(32567)
Path = App.Path
If Right(Path, 1) <> "\" Then
Path = Path & "\"
End If
lRet = GetPrivateProfileSection(Section, strTemp, Len(strTemp), Path & AppName & ".ini")
iPnt = 0
'For Redim+Preserve tables only the las index can be changed
If Left(strTemp, 2) = Chr$(0) & Chr$(0) Then
Exit Function
End If
Do While Left(strTemp, 1) <> Chr$(0)
ReDim Preserve Table(1, iPnt)
iposit = InStr(strTemp, "=")
Table(0, iPnt) = Left$(strTemp, iposit - 1)
strTemp = Mid$(strTemp, iposit + 1)
iposit = InStr(strTemp, Chr$(0))
Table(1, iPnt) = Left$(strTemp, iposit - 1)
strTemp = Mid$(strTemp, iposit + 1)
iPnt = iPnt + 1
Loop
ReDim Table2(iPnt - 1, 1)
For iPnt2 = 0 To iPnt - 1
Table2(iPnt2, 0) = Table(0, iPnt2)
Table2(iPnt2, 1) = Table(1, iPnt2)
Next
GetAllSettings = Table2
End Function
Public Function DeleteSetting(AppName As String, Section As String, Optional Key As String)
Dim lRet As Long
Dim Path As String
Path = App.Path
If Right(Path, 1) <> "\" Then
Path = Path & "\"
End If
If Key = "" Then
lRet = WritePrivateProfileString(Section, vbNullString, vbNullString, Path & AppName & ".ini")
Else
lRet = WritePrivateProfileString(Section, Key, vbNullString, Path & AppName & ".ini")
End If
End Function |
Partager