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
| Sub WriteIniStringFromContent(strSection, strKeyName, ByVal strValue, ByRef strIniContents)
Dim iPosSection, iPosEndSection
' Tagguer les retours chariot / Line Feed / Tab : uniquement si la valeur contient quelque chose
If Not IsNull(strValue) Then
strValue = ReplaceText(strValue, vbAliaLf,"<{LF}>",True,True)
strValue = ReplaceText(strValue, vbCr,"<{CR}>",True,True)
End If
'Find section
iPosSection = InStr(1, strIniContents, "[" & strSection & "]", vbTextCompare)
If iPosSection>0 Then
'Section exists. Find end of section
iPosEndSection = InStr(iPosSection, strIniContents, vbCrLf & "[")
'Is this last section?
If iPosEndSection = 0 Then
iPosEndSection = Len(strIniContents)+1
End If
Dim strOldsContents
If Not IsNull(strKeyName) Then
'Separate section contents
Dim strNewsContents, strLine
Dim strSearchKeyName, bFound
bFound = False
strOldsContents = Mid(strIniContents, iPosSection, iPosEndSection - iPosSection)
strOldsContents = split(strOldsContents, vbCrLf)
'Temp variable To find a Key
strSearchKeyName = LCase(strKeyName & "=")
'Enumerate section lines
For Each strLine In strOldsContents
If LCase(Left(strLine, Len(strSearchKeyName))) = strSearchKeyName Then
bFound = True
' Line found : remove it if strValue is Null
If Not IsNull(strValue) Then
strLine = strKeyName & "=" & strValue
Else
strLine = ""
End If
End If
If Len(strLine)>0 Then
strNewsContents = strNewsContents & strLine & vbCrLf
End If
Next
If Not bFound Then
If Not IsNull(strValue) Then
'key Not found - add it at the end of section
If Len(strNewsContents)>0 Then
If Right(strNewsContents, Len(vbCrlf)) <> vbCrLf And Len(strNewsContents)>0 Then
strNewsContents = strNewsContents & vbCrLf
End If
End If
strNewsContents = strNewsContents & strKeyName & "=" & strValue
End If
End If
Else
' Remove Section
strOldsContents = ""
If Left(Mid(strIniContents, iPosEndSection),Len(vbCrlf)) = vbCrlf Then
' Remove space between 2 section, this one is destroyed
iPosEndSection = iPosEndSection + Len(vbCrlf)
End If
End If
'Combine pre-section, new section And post-section data.
strIniContents = Left(strIniContents, iPosSection-1) & strNewsContents & Mid(strIniContents, iPosEndSection)
Else 'if PosSection>0 Then
'Section Not found. Add section data at the end of file contents.
If Len(strIniContents)>0 Then
If Right(strIniContents, Len(vbCrlf)) <> vbCrLf And Len(strIniContents)>0 Then
strIniContents = strIniContents & vbCrLf
End If
If Not IsNull(strKeyName) Then
' Let cariage return beetween section and keys of last section
strIniContents = strIniContents & vbCrLf
End If
End If
If Not IsNull(strKeyName) Then
strIniContents = strIniContents & "[" & strSection & "]" & vbCrLf
If Not IsNull(strValue) Then
strIniContents = strIniContents & strKeyName & "=" & strValue & vbCrLf
End If
End If
End If
End Sub |
Partager