Click here to Skip to main content
15,916,412 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi, beginner VB user here.

There's an INI file that I want to access and read information from. This is the complete content of the file in question:[^]

My form has a bunch of comboboxes, trackbars and checkboxes. These items needs to be filled by the information taken from the INI file.

Here's how the INI file is formatted;
iSize H=720
iSize W=1280

What I want is make my form items reflect the values in the INI file. For instance,I want CheckBox8 in my form to get checked if bFXAAEnabled has a value of "1" and unchecked if it is "0" vice versa...

So far I couldn't find a way to do it can you please provide me a solution?.
I'm using Visual Basic 2010 and I'm such a noob at VB programming so please explain your code if possible.
Thanks in advance.

OK I found this solution

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.IO
Dim fName As String = (dir + "\My Games\Skyrim\SkyrimPrefs.ini")              'path to text file
Dim testTxt As New StreamReader(fName)
Dim allRead As String = testTxt.ReadToEnd()     'Reads the whole text file to the end
testTxt.Close()                'Closes the text file after it is fully read.
Dim regMatch As String = "bCrosshairEnabled"

If Regex.IsMatch(allRead, regMatch) Then                  'If the match is found in allRead
End If

Now if only i could find a code that does something according to value comes after "=".
Such as:

If bCrosshairEnabled="1" then
End if

Can anybody tell me how to combine this code with the one above to create a solution to my problem?

'Reads each line from the text file one at a time
    For Each line As String In IO.File.ReadLines("text file path")

        'split the string by equals sign
        Dim ary As String() = line.Split("="c)

        'Check the data type of the string we collected is inline with what we are expecting, e.g. numeric
        If IsNumeric(ary(1)) Then

            'create key value pair: the string before the equals and the number after it
            'key = "valuexyz" | value = "36""
            Dim valuePair As New KeyValuePair(Of String, Integer)(ary(0), CInt(ary(1)))

            'obtain the string after the equals sign
            Dim value As Integer = CInt(ary(1))

            'based on the value after the equals sign, do something
            Select Case value
                Case 1
                    ' do something using..
                    'valuePair.Key - this is the string before the equals
                    'valuePair.Value - this is the string after the equals
                Case 2
                    ' do something using..
                    'valuePair.Key - this is the string before the equals
                    'valuePair.Value - this is the string after the equals
                Case Else
                    ' do something using..
                    'valuePair.Key - this is the string before the equals
                    'valuePair.Value - this is the string after the equals
            End Select

        End If

Updated 18-May-12 11:37am
Sergey Alexandrovich Kryukov 18-May-12 14:35pm    
I'm curious: what brings you to reading of such archaic data format? Anyway, what seems to be a problem? Don't know how to read text or what?
theraveman 18-May-12 14:54pm    
I'm making a 3rd party tool that manages settings of a game, better than its vanilla "Options window". The INI file you see is where the settings of that game is stored.

I can normally read the text file and I can read individual lines in text files. But when it comes to that particular ini file, can't read the specific strings. Maybe it's because i'm such a noob lol.

I asked this question in StackOverflow:

But the answers got me nowhere. People often provide codes that is incompatible with VB 2010 so i can't work it out.
WarriorII 29-Jul-12 15:19pm    
Hello Çağan, I have been looking all over the place for a solution to the exact same problem you faced, or are facing... I also consider myself as a noob to programming, been trying to play with it on and off for years now. I run a gaming clan and decided an interface to manipulate the .ini files would be most helpful... Please let me know if you have come accross a solution to this, as I'm eager to get this project on the way. Trying to do this in Visual Basic... Looking forward to hearing from you soon, thank you very much.

1 solution

This task is more or less trivial. You just need to be able to read text files and build some interface on top of it. There is no special support in .NET just because this stuff it too archaic.

Please see this CodeProject article:
An INI file handling class using C#[^].


Think on some reasonable alternative. I would advise to store all configuration-like data in XML file instead of obsolete and non-structured INI, and the best way of doing it would be using Data Contracts.

Please see[^].

Please also see my past answer where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

By the way, another format covered with Data Contract is JSON. Please see:
deseralize a json string array[^].

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900