by The Emporer (4 Submissions)
Category: Files/File Controls/Input/Output
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sun 15th September 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Read and Write procedures for INI files, NO DECLARATIONS procedures only!
API Declarations
NOTE: The file needs to be a full path and the format is 'Key=Value'. These procedures ignore header tags, so if you write a full ini with WriteINI() then it will not have headings in the file, if you want it organized you can add them in manualy and the WriteINI() sub will not write over them.
*Visit my site for VB tools that will help you with your projects.
(http://LostEmpire.s5.com)
Public Function ReadINI(File As String, Key As String) As String
Dim fnum As Integer, Data As String
If (Dir(File) = "") Then Exit Function
fnum = FreeFile
Open File For Input As fnum
Do While Not EOF(fnum)
Line Input #fnum, Data
If (Left(LCase(Data), Len(Key)) = LCase(Key)) Then
ReadINI = Trim(Right(Data, Len(Data) - Len(Key) - 1))
Exit Do
End If
Loop
Close fnum
End Function
Public Sub WriteINI(File As String, Key As String, Value As String)
Dim fn1 As Integer, fn2 As Integer, Data As String, File2 As String, Found As Boolean
If (Dir(File) = "") Then Exit Sub
fn1 = FreeFile
fn2 = fn1 + 1
File2 = Left(File, Len(File) - 4) & ".tmp"
Open File For Input As fn1
Open File2 For Output As fn2
Do While Not EOF(fn1)
Line Input #fn1, Data
If (Left(LCase(Data), Len(Key) + 1) = LCase(Key) & "=") Then
Print #fn2, Key & "=" & Value
Found = True
Else
Print #fn2, Data
End If
Loop
If Not Found Then Print #fn2, Key & "=" & Value
Close fn1, fn2
FileCopy File2, File
Kill File2
End Sub
No comments have been posted about Read and Write procedures for INI files, NO DECLARATIONS procedures only!. Why not be the first to post a comment about Read and Write procedures for INI files, NO DECLARATIONS procedures only!.