Question Load entire section from INI

garriew

Member
Joined
Sep 13, 2013
Messages
13
Programming Experience
Beginner
Hello,
I am using VB.net 2010 and trying to load a section (Words) of an ini file to a text box.

INI File examples
[Settings]
Delay=10
Search=yes
[Words]
Word1=mindless self indulgence
Word2=msi
Word3=nin
etc.

I don't know how many Words keys there will be.

Currently I am using the code as found at the URL below to read/write the ini file.
http://www.vbdotnetforums.com/vb-net-general-discussion/45603-read-write-ini-file.html

Any help will be greatly appreciated.
 
Last edited:
OK, that's good to know.

As to your question, I am not a big fan of using PrivateProfileString and if I were to read this INI file I would use File.ReadAllLines to read the file into a project and then select the relevant information I need. Here is a quick example:-

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  Dim myFileLines() As String = IO.File.ReadAllLines("d:\temp\Settings.ini")
  Dim myListOfWords As New List(Of String)
 
  For Each strWordLine As String In myFileLines.Where(Function(x) x.StartsWith("Word"))
    myListOfWords.Add(strWordLine.Split("="c).Last)
  Next
 
  TextBox1.Text = String.Join(Environment.NewLine, myListOfWords.ToArray)
End Sub


Hope that helps.

Cheers.

Ian
 
The code I posted in the last post of that thread you linked should work no matter how many words there are. In fact I just tried it:

ini.png

To get a particular setting, you can query the collection through Linq:

        Dim MyIniSettings As New IniFile("C:\test.ini")

        Dim WordsSection = From s As IniFile.UserSetting In MyIniSettings.Settings
                           Where s.Section = "Words"
                           Select s
 
Thanks for the help Ian and Herman!

I used Ians code because "went right in" with my current code since I was using the code Mattp gave on the other thread.

Thanks again.
 
Back
Top