-1

Can you help me on this? I want to get the section name and fields of my INI file. Example:

[connection]
server=localhost
user=root
password=root

My program should return the section name and the fields: connection server user password

Thanks in advance..

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 1
    ini files (plain text) are **no longer** used... consider using **application settings** (structured xml), instead. – Phantômaxx Mar 31 '14 at 12:11
  • possible duplicate of [Reading/writing an INI file](http://stackoverflow.com/questions/217902/reading-writing-an-ini-file) – sloth Mar 31 '14 at 12:13
  • @DominicKexel: Not a duplicate, strictly speaking. VB.NET and C# are different languages, although they use the same platform (.NET). – Victor Zakharov Mar 31 '14 at 13:33

1 Answers1

0

Just in case someone needs it. Here it is:

   Dim path As String = Application.StartupPath & "\path_to_file"

' get a list of the files in this directory
' -----------------------------------------

    Dim file_list As String() = Directory.GetFiles(path, "*.ini")

' go through the list of files 
' -------------------------------------------------------------------
  For Each f As String In file_list

    Dim a = New System.IO.FileInfo(f).Name

    ' open the file and read it
    ' -------------------------
    Dim sr As StreamReader = New StreamReader(f)

    ' read through the file line by line
    ' -----------------------------------
    Do While sr.Peek() >= 0
        Dim temp_name = Split(sr.ReadLine(), "=")
        first_part = temp_name(0)
        second_part = temp_name(1)

        ' do something with the information here

    Loop

    sr.Close()

  Next
John
  • 1,310
  • 3
  • 32
  • 58
  • What about this test case `setting="Val=ue"`? – Victor Zakharov Mar 31 '14 at 13:35
  • If it is a true ini file, then you have to assume it is formatted correctly. He's simply asking how to read it, not how to validate the format. – John Mar 31 '14 at 13:51
  • I dont see anything wrong with the above value. From my understanding it is perfectly valid and as such, your parser needs to be able to read it. If you find an INI format specification, that tells specifically the above is NOT an accepted scenario, that would help. – Victor Zakharov Mar 31 '14 at 13:53