How to Enable Write in .INI File in Visual Basic 6.0
Option Explicit
Public Function WriteIniValue(INIpath As String, PutKey As String, PutVariable As String, PutValue As String)
Dim temp As String
Dim LcaseTemp As String
Dim ReadKey As String
Dim ReadVariable As String
Dim LOKEY As Integer
Dim HIKEY As Integer
Dim KEYLEN As Integer
Dim VAR As Integer
Dim VARENDOFLINE As Integer
Dim NF As Integer
Dim X As Integer
AssignVariables:
NF = FreeFile
ReadKey = vbCrLf & "[" & LCase$(PutKey) & "]" & Chr$(13)
KEYLEN = Len(ReadKey)
ReadVariable = Chr$(10) & LCase$(PutVariable) & "="
EnsureFileExists:
Open INIpath For Binary As NF
Close NF
SetAttr INIpath, vbArchive
LoadFile:
Open INIpath For Input As NF
temp = Input$(LOF(NF), NF)
temp = vbCrLf & temp & "[]"
Close NF
LcaseTemp = LCase$(temp)
LogicMenu:
LOKEY = InStr(LcaseTemp, ReadKey)
If LOKEY = 0 Then GoTo AddKey:
HIKEY = InStr(LOKEY + KEYLEN, LcaseTemp, "[")
VAR = InStr(LOKEY, LcaseTemp, ReadVariable)
If VAR > HIKEY Or VAR < LOKEY Then GoTo AddVariable:
GoTo RenewVariable:
AddKey:
temp = Left$(temp, Len(temp) - 2)
temp = temp & vbCrLf & vbCrLf & "[" & PutKey & "]" & vbCrLf & PutVariable & "=" & PutValue
GoTo TrimFinalString:
AddVariable:
temp = Left$(temp, Len(temp) - 2)
temp = Left$(temp, LOKEY + KEYLEN) & PutVariable & "=" & PutValue & vbCrLf & Mid$(temp, LOKEY + KEYLEN + 1)
GoTo TrimFinalString:
RenewVariable:
temp = Left$(temp, Len(temp) - 2)
VARENDOFLINE = InStr(VAR, temp, Chr$(13))
temp = Left$(temp, VAR) & PutVariable & "=" & PutValue & Mid$(temp, VARENDOFLINE)
GoTo TrimFinalString:
TrimFinalString:
temp = Mid$(temp, 2)
Do Until InStr(temp, vbCrLf & vbCrLf & vbCrLf) = 0
temp = Replace(temp, vbCrLf & vbCrLf & vbCrLf, vbCrLf & vbCrLf)
Loop
Do Until Right$(temp, 1) > Chr$(13)
temp = Left$(temp, Len(temp) - 1)
Loop
Do Until Left$(temp, 1) > Chr$(13)
temp = Mid$(temp, 2)
Loop
OutputAmendedINIFile:
Open INIpath For Output As NF
Print #NF, temp
Close NF
End Function
Enjoy the code!