Creating a Config.INI File in VB6.0

In the Module File I name it INIWrite.bas Here is the code: Author: kevern Copyright © 2009 Kevern Solutions
  1. Option Explicit
  2.  
  3. Public Function WriteIniValue(INIpath As String, PutKey As String, PutVariable As String, PutValue As String)
  4. Dim temp As String
  5. Dim LcaseTemp As String
  6. Dim ReadKey As String
  7. Dim ReadVariable As String
  8. Dim LOKEY As Integer
  9. Dim HIKEY As Integer
  10. Dim KEYLEN As Integer
  11. Dim VAR As Integer
  12. Dim VARENDOFLINE As Integer
  13. Dim NF As Integer
  14. Dim X As Integer
  15.  
  16. AssignVariables:
  17.     NF = FreeFile
  18.     ReadKey = vbCrLf & "[" & LCase$(PutKey) & "]" & Chr$(13)
  19.     KEYLEN = Len(ReadKey)
  20.     ReadVariable = Chr$(10) & LCase$(PutVariable) & "="
  21.        
  22. EnsureFileExists:
  23.     Open INIpath For Binary As NF
  24.     Close NF
  25.     SetAttr INIpath, vbArchive
  26.    
  27. LoadFile:
  28.     Open INIpath For Input As NF
  29.     temp = Input$(LOF(NF), NF)
  30.     temp = vbCrLf & temp & "[]"
  31.     Close NF
  32.     LcaseTemp = LCase$(temp)
  33.    
  34. LogicMenu:
  35.     LOKEY = InStr(LcaseTemp, ReadKey)
  36.     If LOKEY = 0 Then GoTo AddKey:
  37.     HIKEY = InStr(LOKEY + KEYLEN, LcaseTemp, "[")
  38.     VAR = InStr(LOKEY, LcaseTemp, ReadVariable)
  39.     If VAR > HIKEY Or VAR < LOKEY Then GoTo AddVariable:
  40.     GoTo RenewVariable:
  41.    
  42. AddKey:
  43.         temp = Left$(temp, Len(temp) - 2)
  44.         temp = temp & vbCrLf & vbCrLf & "[" & PutKey & "]" & vbCrLf & PutVariable & "=" & PutValue
  45.         GoTo TrimFinalString:
  46.        
  47. AddVariable:
  48.         temp = Left$(temp, Len(temp) - 2)
  49.         temp = Left$(temp, LOKEY + KEYLEN) & PutVariable & "=" & PutValue & vbCrLf & Mid$(temp, LOKEY + KEYLEN + 1)
  50.         GoTo TrimFinalString:
  51.        
  52. RenewVariable:
  53.         temp = Left$(temp, Len(temp) - 2)
  54.         VARENDOFLINE = InStr(VAR, temp, Chr$(13))
  55.         temp = Left$(temp, VAR) & PutVariable & "=" & PutValue & Mid$(temp, VARENDOFLINE)
  56.         GoTo TrimFinalString:
  57.  
  58. TrimFinalString:
  59.         temp = Mid$(temp, 2)
  60.         Do Until InStr(temp, vbCrLf & vbCrLf & vbCrLf) = 0
  61.         temp = Replace(temp, vbCrLf & vbCrLf & vbCrLf, vbCrLf & vbCrLf)
  62.         Loop
  63.    
  64.         Do Until Right$(temp, 1) > Chr$(13)
  65.         temp = Left$(temp, Len(temp) - 1)
  66.         Loop
  67.    
  68.         Do Until Left$(temp, 1) > Chr$(13)
  69.         temp = Mid$(temp, 2)
  70.         Loop
  71.    
  72. OutputAmendedINIFile:
  73.         Open INIpath For Output As NF
  74.         Print #NF, temp
  75.         Close NF
  76. End Function
  77.  
  78. This is the INIRead File I name it INIRead.bas
  79. Here is the Code:
  80. Option Explicit
  81.  
  82. Public Function ReadIniValue(INIpath As String, KEY As String, Variable As String) As String
  83. Dim NF As Integer
  84. Dim temp As String
  85. Dim LcaseTemp As String
  86. Dim ReadyToRead As Boolean
  87.    
  88. AssignVariables:
  89.         NF = FreeFile
  90.         ReadIniValue = ""
  91.         KEY = "[" & LCase$(KEY) & "]"
  92.         Variable = LCase$(Variable)
  93.    
  94. EnsureFileExists:
  95.     Open INIpath For Binary As NF
  96.     Close NF
  97.     'SetAttr INIpath, vbArchive
  98.    
  99. LoadFile:
  100.     Open INIpath For Input As NF
  101.     While Not EOF(NF)
  102.     Line Input #NF, temp
  103.     LcaseTemp = LCase$(temp)
  104.     If InStr(LcaseTemp, "[") <> 0 Then ReadyToRead = False
  105.     If LcaseTemp = KEY Then ReadyToRead = True
  106.     If InStr(LcaseTemp, "[") = 0 And ReadyToRead = True Then
  107.         If InStr(LcaseTemp, Variable & "=") = 1 Then
  108.             ReadIniValue = Mid$(temp, 1 + Len(Variable & "="))
  109.             Close NF: Exit Function
  110.             End If
  111.         End If
  112.     Wend
  113.     Close NF
  114. End Function

You can have more sample code in my blog, just take a visit here: http://jackjones2010.blogspot.com/2010/07/creating-configini-file-in-vb60.html Thanks and Good Luck!

Great module that produces a neatly formatted INI file. I have tried it both ways: using the API, and using this method. Of the two methods this one produces the most aesthetically pleasing file. A file built with this module has blank lines separating sections, a very nice touch. If using the API the sections have no spacers between them and appear cramped. Of course VB6 doesn't care, but for me this method looks better. This method does have a shortcoming, albeit a minor one, in that there are no methods for deleting sections or keys; but that is easily done by using the API. Great code, Kevern!

I wonder what is the method to automatically poll key strokes from outside vb applications. and if any source program to this use. Thanks Mike Kim

I am an experience VB Programmer, I don't take advices from other authors, I created this code w/o any help from others. Keep that in mind!

I forgot to mention that I downloaded this code a few years ago and it was all neatly wrapped up in a .BAS file at that time; so I just downloaded the .BAS file and put it in my project. Looking at the code posted above there are some small differences between it and the older code, and it is no longer in a .BAS file: you will have to do that yourself. I have had such good luck with it that I decided to add my voice as a programmer who is appreciative of this module.

Add new comment