Get the label names for a given ini section

Posted on 2003-02-25
Medium Priority
Last Modified: 2010-05-01
I would like to get the names of all the keys in a given section of an ini file. For example this is part of the ini file;

Make = Ford
Model = Thunderbird
Year = 1995

SQFT = 1500
Stories = 2

I can get the key values but what I want to do is build an array of the keys themselves without knowing in advance what the key names are. I.e. "Make,Model,Year"
Question by:blackfirerca
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8018367
Well, you can do this

dim Array1() as string

Once you know how many there are (use a counter as you input the file), you can

Redim Array1(1 to Counter) as string

and then let the values of that array equal what you have inputed.

Author Comment

ID: 8018511
I know how to build an array, what I'm after is a way to get the key names of the section. For instance I know there is a section [AUTO] but I don't know what keys are in that section or how many keys there are. How do I get the key names into the array? Then I can go back and get the values.

Expert Comment

ID: 8018607
Possible Idea:  It all depends on what you are trying to accomplish

Make = Ford
Model = Thunderbird
Year = 1995

SQFT = 1500
Stories = 2

I had recently updated an application for a team and made the INI file the driver.  The application was generic and the business area would only have to update the ini file.  I did designed it as a hierarchy.  Every section defined and sections that belonged to it through a keys key/value pair.  I then used the Split function to split the string of comma delimited data into an array.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 8018804
by using the below code you can get a particular value from the main topic(house/Auto)

Public 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

Public Function Read_Settings_Ini(Str_Application As String, Str_Key As String, Str_Filename As String) As String
On Error GoTo errormsg

        Dim Str_Temp As String
        Str_Temp = Space(255)
        GetPrivateProfileString Str_Application, Str_Key, Str_Temp, Str_Temp, 255, Str_Filename
        Read_Settings_Ini = Str_Temp

Exit Function
        Read_Settings_Ini = Str_Temp
End Function.

Private sub Form_Load()
Arr(0) = Read_Settings_Ini("AUTO", "MAKE", App.Path & "\SETTINGS.INI")
Arr(1) = Read_Settings_Ini("AUTO", "MODEL", App.Path & "\SETTINGS.INI")

End Sub

and so on .....


LVL 16

Expert Comment

ID: 8018810
try this little example:

Private Sub Form_Load()
Dim sPath As String, strLine As String, idx As Integer
Dim i As Integer, bolOut As Boolean
Dim arrKeys() As String

sPath = "C:\A_Project\ee\read keynames from ini\keynames.ini"
i = FreeFile
Open sPath For Input As #i
        Line Input #i, strLine
        If strLine = "[AUTO]" Then
            Do While Not EOF(i)
                Line Input #1, strLine
                If strLine = "" Or Left$(strLine, 1) = "[" Then
                    bolOut = True
                    Exit Do
                End If
                ReDim Preserve arrKeys(idx)
                arrKeys(idx) = Left$(strLine, InStr(1, strLine, "=", vbTextCompare) - 1)
                idx = idx + 1
        End If
    Loop Until bolOut = True
Close #i

End Sub

Accepted Solution

JMoon5FTM earned 300 total points
ID: 8018875
There is a Windows function called GetPrivateProfile section which retrieves all keys and values within a section.  Use is:

Private Declare Function GetPrivateProfileSection lib "kernel32" alias "GetPrivateProfileSectionA" (byval SectionName as String, byval SectionReturn as String, byval StrLen as Long, byval Filename as String) as Long

Dim IniSection as String, IniLines() as String, Line, Key as String, Value as String, EqPos as Long
IniSection = String$(0, 1023) 'or however much space you think you need
IniSection = Left$(IniSection, GetPrivateProfileSection("AUTO", IniSection, 1024, "myfile.ini")
IniLines = Split(IniSection, vbcrlf)
for each Line in IniLines
   if left$(trim$(line), 1) <> ";" then
      EqPos = instr(Line, "=")
      Key = left$(Line, EqPos - 1)
      Value = Mid$(Line, EqPos + 1)
   end if
next line

Under Win NT/2000/XP, the "if left$..." isn't necessary, because these Windows versions strip comments from the section.  However, under Win 95/98/ME, the comments are returned, and you have to be able to handle them.
LVL 16

Expert Comment

ID: 8025291
What's the problem?, if the comment is what you need, why did you punish JMoon5FTM with a "B" grade???

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question