Click here to Skip to main content
15,896,154 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: http://www.heypasteit.com/clip/0C3Q[^]

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;
...
[Display]
bCrosshairEnabled=1
bDoDepthOfField=0
bFXAAEnabled=1
uiMaxSkinnedTreesToRender=10
iSize H=720
iSize W=1280
[Controls]
bGamepadEnable=1
...


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.

UPDATE 1
OK I found this solution

VB
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
    'DO SOMETHING
Else
    'DO SOMETHING ELSE
End If


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

VB
If bCrosshairEnabled="1" then
'DO THIS
ELSE
'DO THAT
End if


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

VB
'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
            'e.g.
            '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

    Next
Posted
Updated 18-May-12 11:37am
v7
Comments
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?
--SA
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: http://stackoverflow.com/questions/10649514/edited-searching-for-a-specific-string-in-a-text-file-vb-net-2010

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#[^].

[EDIT]

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 http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

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[^].

—SA
 
Share this answer
 
v2

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