Get the label names for a given ini section

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;

[AUTO]
Make = Ford
Model = Thunderbird
Year = 1995

[HOME]
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"
blackfirercaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JacamarCommented:
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.
0
blackfirercaAuthor Commented:
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.
0
BurtonRacingCommented:
Possible Idea:  It all depends on what you are trying to accomplish


[AUTO]
Keys=Make,Model,Year
Make = Ford
Model = Thunderbird
Year = 1995

[HOME]
Keys=SQFT,Stories
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jayeshshahCommented:
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
errormsg:
        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 .....



K'Regards

Jayesh
0
Richie_SimonettiIT OperationsCommented:
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
    Do
        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
            Loop
           
        End If
    Loop Until bolOut = True
   
Close #i

End Sub
0
JMoon5FTMCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richie_SimonettiIT OperationsCommented:
What's the problem?, if the comment is what you need, why did you punish JMoon5FTM with a "B" grade???
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.