Solved VB 2010 how to populate a listbox from an ini file

JBmorris

--
Member
VIP
Local time
12:16 AM
Messages
145
Location
Madison, WI
Hello SF users! Okay so I am making a program that involves listboxes and ini files. What I am trying to do is populate the listbox from an ini file(make the listbox show strings from a section). I don't have any code yet so... How do I do this?
 

My Computer My Computer

At a glance

Windows Vista Business 32bitIntel(R) Pentium(R) Daul CPU E2220 @ 2.40GHz2.00GBIntel Pentium
Computer type
PC/Desktop
Computer Manufacturer/Model Number
HP
OS
Windows Vista Business 32bit
CPU
Intel(R) Pentium(R) Daul CPU E2220 @ 2.40GHz
Memory
2.00GB
Graphics Card(s)
Intel Pentium
Sound Card
AudioESP SoundMAX
Monitor(s) Displays
Dell
Hard Drives
ST380815AS ATA Device 80 GB
Keyboard
Logitech Keyboard
Mouse
Logitech Mouse
Internet Speed
44 Mbps
Antivirus
AVG Free

My Computer My Computer

At a glance

Windows 7 Pro 32Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz4,00 GB (Usable 2,98)NVIDIA NVS 5100M
Computer type
Laptop
Computer Manufacturer/Model Number
HP Elitebook 8540p
OS
Windows 7 Pro 32
CPU
Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz
Motherboard
Hewlett-Packard 1521
Memory
4,00 GB (Usable 2,98)
Graphics Card(s)
NVIDIA NVS 5100M
Sound Card
NVIDIA High Definition Audio
Screen Resolution
1600x900
Hard Drives
INTEL SSDSA2CW120G3
Antivirus
F-Secure Internet Security
Browser
IE, Firefox, Opera
Other Info
Sandboxie,
SRP (Software Restriction Policy),
EMET (Enhanced Mitigation Experience Toolkit),
WFC (Windows Firewall Control by BiniSoft),
Malwarebytes Premium
Sorry, I missed the fact that that code didn't look up a specific section. So forget that and try this instead:

Code:
Dim inifile As String = System.AppDomain.CurrentDomain.BaseDirectory() & Replace(LCase(IO.Path.GetFileName(Application.ExecutablePath)), ".exe", ".ini")
Dim objReader As New System.IO.StreamReader(inifile, System.Text.Encoding.Default)
Dim row As String
Dim section As String = "Links"
Dim match As Boolean = False
Do While objReader.Peek() <> -1
    row = objReader.ReadLine()
    If LCase(row) = "[" & LCase(section) & "]" Then
        match = True
    ElseIf match AndAlso Trim(row) <> "" AndAlso Mid(row, 1, 1) <> "[" Then
        Dim data() As String = Split(row, "=")
        Dim key = data(0)
        Dim value = data(1)
        Debug.Print(key & "=" & value)
        'listbox.Items.Add(key & "=" & value)
    Else
        match = False
    End If
Loop
objReader.Close()
Change the value for inifile to the path and filename to it. The value for it above is something I use to avoid having hard coded paths and file names. It takes the current directory and the name of the exe and changes exe to ini. When you run the program from VB it means the bin folder in your project folder. So it looks for an ini file with the same name as the exe file, in the "current directory".
Then set the value for section. I've added a commented example of how to add the key and value to a listbox.

The code will read line by line looking for the section. After it finds it it will read the following key/value lines until it reaches either an empty line or a new section - row value beginning with [
 

My Computer My Computer

At a glance

Windows 7 Pro 32Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz4,00 GB (Usable 2,98)NVIDIA NVS 5100M
Computer type
Laptop
Computer Manufacturer/Model Number
HP Elitebook 8540p
OS
Windows 7 Pro 32
CPU
Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz
Motherboard
Hewlett-Packard 1521
Memory
4,00 GB (Usable 2,98)
Graphics Card(s)
NVIDIA NVS 5100M
Sound Card
NVIDIA High Definition Audio
Screen Resolution
1600x900
Hard Drives
INTEL SSDSA2CW120G3
Antivirus
F-Secure Internet Security
Browser
IE, Firefox, Opera
Other Info
Sandboxie,
SRP (Software Restriction Policy),
EMET (Enhanced Mitigation Experience Toolkit),
WFC (Windows Firewall Control by BiniSoft),
Malwarebytes Premium
Sorry, I missed the fact that that code didn't look up a specific section. So forget that and try this instead:

Code:
Dim inifile As String = System.AppDomain.CurrentDomain.BaseDirectory() & Replace(LCase(IO.Path.GetFileName(Application.ExecutablePath)), ".exe", ".ini")
Dim objReader As New System.IO.StreamReader(inifile, System.Text.Encoding.Default)
Dim row As String
Dim section As String = "Links"
Dim match As Boolean = False
Do While objReader.Peek() <> -1
    row = objReader.ReadLine()
    If LCase(row) = "[" & LCase(section) & "]" Then
        match = True
    ElseIf match AndAlso Trim(row) <> "" AndAlso Mid(row, 1, 1) <> "[" Then
        Dim data() As String = Split(row, "=")
        Dim key = data(0)
        Dim value = data(1)
        Debug.Print(key & "=" & value)
        'listbox.Items.Add(key & "=" & value)
    Else
        match = False
    End If
Loop
objReader.Close()
Change the value for inifile to the path and filename to it. The value for it above is something I use to avoid having hard coded paths and file names. It takes the current directory and the name of the exe and changes exe to ini. When you run the program from VB it means the bin folder in your project folder. So it looks for an ini file with the same name as the exe file, in the "current directory".
Then set the value for section. I've added a commented example of how to add the key and value to a listbox.

The code will read line by line looking for the section. After it finds it it will read the following key/value lines until it reaches either an empty line or a new section - row value beginning with [
Okay I will try this code. and sorry for late reply...
 

My Computer My Computer

At a glance

Windows Vista Business 32bitIntel(R) Pentium(R) Daul CPU E2220 @ 2.40GHz2.00GBIntel Pentium
Computer type
PC/Desktop
Computer Manufacturer/Model Number
HP
OS
Windows Vista Business 32bit
CPU
Intel(R) Pentium(R) Daul CPU E2220 @ 2.40GHz
Memory
2.00GB
Graphics Card(s)
Intel Pentium
Sound Card
AudioESP SoundMAX
Monitor(s) Displays
Dell
Hard Drives
ST380815AS ATA Device 80 GB
Keyboard
Logitech Keyboard
Mouse
Logitech Mouse
Internet Speed
44 Mbps
Antivirus
AVG Free
Hmm, I am not sure if i did this right...
Code:
Dim inifile As String = System.AppDomain.CurrentDomain.BaseDirectory() & Replace(LCase(IO.Path.GetFileName(Application.ExecutablePath)), "WindowsApplication2.exe", "IDstorage.ini") <here is what i am confused on.
        Dim objReader As New System.IO.StreamReader(inifile, System.Text.Encoding.Default)
        Dim row As String
        Dim section As String = "list"
        Dim match As Boolean = False
        Do While objReader.Peek() <> -1
            row = objReader.ReadLine()
            If LCase(row) = "[" & LCase(section) & "]" Then
                match = True
            ElseIf match AndAlso Trim(row) <> "" AndAlso Mid(row, 1, 1) <> "[" Then
                Dim data() As String = Split(row, "=")
                Dim key = data(0)
                Dim value = data(1)
                Debug.Print(key & "=" & value)
                ListBox1.Items.Add(key & "=" & value)
            Else
                match = False
            End If
        Loop
        objReader.Close()
am i supposed to put in this
My.Application.Info.DirectoryPath + "/inifle.ini"
for the ini value?
 

My Computer My Computer

At a glance

Windows Vista Business 32bitIntel(R) Pentium(R) Daul CPU E2220 @ 2.40GHz2.00GBIntel Pentium
Computer type
PC/Desktop
Computer Manufacturer/Model Number
HP
OS
Windows Vista Business 32bit
CPU
Intel(R) Pentium(R) Daul CPU E2220 @ 2.40GHz
Memory
2.00GB
Graphics Card(s)
Intel Pentium
Sound Card
AudioESP SoundMAX
Monitor(s) Displays
Dell
Hard Drives
ST380815AS ATA Device 80 GB
Keyboard
Logitech Keyboard
Mouse
Logitech Mouse
Internet Speed
44 Mbps
Antivirus
AVG Free
This will be just fine:

Dim inifile As String = My.Application.Info.DirectoryPath + "/inifle.ini"

or even safer a hard coded value will work(as long as it's correct):

Dim inifile As String = "c:\foldername\filename.ini"
 

My Computer My Computer

At a glance

Windows 7 Pro 32Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz4,00 GB (Usable 2,98)NVIDIA NVS 5100M
Computer type
Laptop
Computer Manufacturer/Model Number
HP Elitebook 8540p
OS
Windows 7 Pro 32
CPU
Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz
Motherboard
Hewlett-Packard 1521
Memory
4,00 GB (Usable 2,98)
Graphics Card(s)
NVIDIA NVS 5100M
Sound Card
NVIDIA High Definition Audio
Screen Resolution
1600x900
Hard Drives
INTEL SSDSA2CW120G3
Antivirus
F-Secure Internet Security
Browser
IE, Firefox, Opera
Other Info
Sandboxie,
SRP (Software Restriction Policy),
EMET (Enhanced Mitigation Experience Toolkit),
WFC (Windows Firewall Control by BiniSoft),
Malwarebytes Premium
This will be just fine:

Dim inifile As String = My.Application.Info.DirectoryPath + "/inifle.ini"

or even safer a hard coded value will work(as long as it's correct):

Dim inifile As String = "c:\foldername\filename.ini"
It works!
Thanks Tookeri!:D
 

My Computer My Computer

At a glance

Windows Vista Business 32bitIntel(R) Pentium(R) Daul CPU E2220 @ 2.40GHz2.00GBIntel Pentium
Computer type
PC/Desktop
Computer Manufacturer/Model Number
HP
OS
Windows Vista Business 32bit
CPU
Intel(R) Pentium(R) Daul CPU E2220 @ 2.40GHz
Memory
2.00GB
Graphics Card(s)
Intel Pentium
Sound Card
AudioESP SoundMAX
Monitor(s) Displays
Dell
Hard Drives
ST380815AS ATA Device 80 GB
Keyboard
Logitech Keyboard
Mouse
Logitech Mouse
Internet Speed
44 Mbps
Antivirus
AVG Free
Back
Top