Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

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;

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"
1 Solution
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.
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.
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.

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 .....


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
        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
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.
Richie_SimonettiIT OperationsCommented:
What's the problem?, if the comment is what you need, why did you punish JMoon5FTM with a "B" grade???

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now