CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2007
    Posts
    68

    [RESOLVED] Counting the number of items under a section in an ini file.

    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 by 1druid1; March 29th, 2012 at 11:57 AM.

  2. #2
    Join Date
    Apr 2007
    Posts
    68

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

    Hi

    I am a little further on

    Code:
     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

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

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

    Hi. Would it be possible to post your INI file here, so that we can also play with it. Perhaps someone can then come up with a solution.

  4. #4
    Join Date
    Apr 2007
    Posts
    68

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

    Hi

    The ini file is very basic.

    [Setup]
    Database="Database Location"

    [MySection1]
    Item1 = ""
    Item2 = ""
    Item3 = ""

    [MySection2]
    Item1 = ""
    Item2 = ""
    Item3 = ""
    Item4 = ""

    [MySection3]
    Item1 = ""
    Item2 = ""
    Item3 = ""
    Item4 = ""
    Item5 = ""
    Item6 = ""

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

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

    Look at this logic:

    Code:
              Label2.Text = 0 
              Do While r.EndOfStream = False 
                   If r.ReadLine = "[MySection2]" Then ' Only for a section
                       Do While r.EndOfStream = False ' if you have more
                            If r.ReadLine.StartsWith("Item") Then  ' and it starts with...
                                Label2.Text = Label2.Text + 1 
                            Else 
                                r.ReadAll() ' Skip to end of file 
                           End If 
                        Loop 
                   End If 
                Loop
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured