READING .INI FILES FROM WITHIN VB PROGRAM

Does anyone have any nifty functions for reading a parameter out of an .INI file?  Eventually, one to write a value to an .INI file would also be useful, but now the focus is on reading one to determine a path string.  Easy enough to load the .INI file into a memvar and do it myself, but I know I've seen VB functions that use an API call to handle this quickly and efficienty.
LVL 1
DreamingEagleAsked:
Who is Participating?
 
VbmasterConnect With a Mentor Commented:
You can use this code, it's a class file so you can put it in a file and rename it to {something}.cls

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "clsINI"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
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 GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationname As String, ByVal lpKeyName As String, ByVal nDefault As Long, 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
Private Declare Function GetProfileInt Lib "kernel32" Alias "GetProfileIntA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal nDefault As Long) As Long
Private Declare Function GetProfileSection Lib "kernel32" Alias "GetProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Private 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
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 WriteProfileSection Lib "kernel32" Alias "WriteProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String) As Long
Private Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long

Sub INIGetSections(Filename As String, x() As String)

  Dim Filenr As Integer
  Dim Rad As String
  Dim Count As Long
 
  Filenr = FreeFile
  Open Filename For Input As #Filenr
  Do Until EOF(1)
    Line Input #Filenr, Rad
    If (left$(Rad, 1) = "[") And (Right$(Rad, 1) = "]") Then
      Count = Count + 1
      ReDim Preserve x(Count)
      x(Count) = Mid$(Rad, 2, Len(Rad) - 2)
    End If
  Loop
  Close #Filenr
 
End Sub
Function INIRead(sFilename As String, sSection As String, sKeyName As String) As String
 
  Dim sRet As String
 
  sRet = String(255, Chr$(0))
  INIRead = left$(sRet, GetPrivateProfileString(sSection, ByVal sKeyName, "", sRet, Len(sRet), sFilename))

End Function

Sub INIWrite(sFilename As String, sSection As String, sKeyName As String, sValue As String)
 
  Call WritePrivateProfileString(sSection, sKeyName, sValue, sFilename)

End Sub
Sub INIReadSection(Filename As String, Section As String, ByRef x() As String)

  Dim Filenr As Integer
  Dim xCount As Integer
  Dim Rad As String
 
  Filenr = FreeFile
  Open Filename For Input Lock Read As #Filenr
  Do Until EOF(Filenr) Or (Rad = Section)
    Line Input #Filenr, Rad
  Loop
  Do Until EOF(Filenr) Or (Len(Rad) = 0)
    Line Input #Filenr, Rad
    If (Len(Rad) > 0) Then
      If (xCount Mod 10 = 0) Then ReDim Preserve x(xCount + 10)
      xCount = xCount + 1
      x(xCount) = Rad
    End If
  Loop
  Close #Filenr
  ReDim Preserve x(xCount)

End Sub
0
 
oohchildCommented:
Actually could there be a better way instead of reading from a .ini file to retrieve the path.
0
 
Erick37Commented:
Which path?
App.Path?
0
 
VbmasterCommented:
What I do in my program is that I store the path to the program in the registry. That way my update program can find my programs easy and see for further information in the .INI file. Don't like to keep to much info in the registry. But if you only need to store the path to ya program there, registry could be a good thing.

If this is the case you can check out the class which I use for registry stuff, I uploaded it to

http://www.cyd.liu.se/~freqv416/clsRegistry.cls


oohchild: 'The path'? You mean the path of where the program you are running right now is? Check out the App structure. App.Path tells you the path where your exe is...
0
 
kbalarajuCommented:
You can create a Settings table for your Applcation having columns Setting,TextValue,IntValue,FloatValue. You can store all the settigns in to this table & retrieve whenever needed.
0
All Courses

From novice to tech pro — start learning today.