Counting the number of items under a section in an ini file

DouglasBell

Active member
Joined
Aug 16, 2011
Messages
38
Programming Experience
Beginner
Hi all

Looking for a little help, I have a display system that I created in VS Studio 2005 that uses ini files for certain functions. Reading and writing to ini files is easy enough but what I just can't figure out is how to count the number of items under a particular ini section. Any help is appreciated

Cheers Dougie
 
Last edited:
That is relativity easy, when you are reading from the *.ini file just look for the '[', start counting the items ignoring any lines that are blank. When you reach the second '[' you have come to the start of the next section, of course when you have come to the end of the file this is also the end of the current section.

Regards
 
Hi
I am a little further on
VB.NET:
 Private Sub ReadIni2() 
        Using r As System.IO.StreamReader = New System.IO.StreamReader("c:\Myinifile.ini") 

            Label2.Text = 0 
            Do While r.EndOfStream = False 
                If r.ReadLine = "[MySection2]" Then 
                    Do While r.EndOfStream = False 
                        If r.ReadLine.StartsWith("Item") Then 
                            Label2.Text = Label2.Text + 1 

                        Else 
                        End If 
                    Loop 
                End If 
            Loop 
        End Using 
    End Sub
Basically the above reads through the whole ini file, when it gets to the [Mysection2] it then counts all the items beginning with "Item". So it sort of works, unfortuantly it also counts any other items beginning with "Item" from that point on, what I need is it to stop counting at the end of the specified section.
Cheers
Dougie
 
Hi
I am a little further on
VB.NET:
 Private Sub ReadIni2()[INDENT][COLOR=#b22222]Dim EndOfITems as Boolean = False
Dim CurrentLine as String = ""
[/COLOR][/INDENT]
        Using r As System.IO.StreamReader = New System.IO.StreamReader("c:\Myinifile.ini") 
            Label2.Text = 0 
            Do While r.EndOfStream = False [COLOR=#b22222]And Not EndOfITems[/COLOR]
                If r.ReadLine = "[MySection2]" Then 
                    Do While r.EndOfStream = False [COLOR=#b22222]And Not EndOfITems[/COLOR][INDENT=3][COLOR=#b22222] CurrentLine = r.ReadLine[/COLOR] [/INDENT]
                        If [COLOR=#b22222]CurrentLine[/COLOR].StartsWith("Item") Then 
                            Label2.Text = Label2.Text + 1 
                        Else[INDENT=3][COLOR=#b22222]If CurrentLine.StartsWith("[") Then[/COLOR][/INDENT]
[INDENT=4][COLOR=#b22222]EndOfItems = True[/COLOR][/INDENT]
[INDENT=3][COLOR=#b22222]End If[/COLOR][/INDENT]
                        End If 
                    Loop 
                End If 
            Loop 
        End Using 
    End Sub

In that case the code added to yours should sort the problem out. It will stop either at the EOF or when it Finds a '[' (The start of the next Section). Sorry but the formatting went screwy.

Hope this helps.
 
Back
Top