1

There's an INI file that I want to access and read information from. This is the complete content of the INI file in question: http://www.heypasteit.com/clip/0C3Q

Several people suggested me codes but they don't work. I believe it is because of the [ ] tags in that INI file. Because they work if I remove the tags.

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

For instance, ini file has these lines;

...
bCrosshairEnabled=1
bDoDepthOfField=0
bFXAAEnabled=1
uiMaxSkinnedTreesToRender=10
iSize H=720
iSize W=1280
...

Example: I want the checkbox8 in my form to get checked if bFXAAEnabled has a value of 1 or unchecked if it is 0.

Please make sure your code is compatible VB.NET 2010.

5 Answers 5

3

You can try this

    Dim myvalue As Integer
    For Each k As String In IO.File.ReadLines("d:\test.txt")
        If k.Contains("valueccc=") Then
            Dim values() As String = k.Split(CChar("=")).ToArray
            myvalue = Convert.ToInt32(values(1))
        End If
    Next
Select case myvalue
case 1
case 2
End select
Sign up to request clarification or add additional context in comments.

8 Comments

This won't compile with Option Strict On!
Edited, it should now be able to support strict on. Thank you.
Please try again, there is .ToArray.
This is the error message i'm getting: 'ToArray' is not a member of 'System.Array'. I use VB.NET 2010
Plus there is no "ReadLines" but "ReadAllLines"... I don't know what's going on. I think VB.NET 2010 is not compatible with your codes.
|
1
'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

3 Comments

But how do i know whether the number is from valuexyz or valueabc?
@Çağan That's not what you asked. Improve the quality of your question and you'll get more useful answers...
Your code works great when reading from plain text file. But it won't work when reading from this: heypasteit.com/clip/0C3Q. I believe the reason is the "[]" tags in the file. How can I overcome this?
1

Inferred from your comments to my other answer I am posting a new answer on how to read values from an INI file:

Imports System.Text
Imports System.Runtime.InteropServices

Public Class TestForm
    'declare the API
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, _
                        ByVal lpKeyName As String, _
                        ByVal lpDefault As String, _
                        ByVal lpReturnedString As StringBuilder, _
                        ByVal nSize As Integer, _
                        ByVal lpFileName As String) As Integer
    End Function

    'Function to retrieve a value from an INI file
    Public Function GetINIValue(filename As String, section As String, key As String, Optional defaultValue As String = "") As String
        Dim res As Integer
        Dim sb As New StringBuilder(500)
        res = GetPrivateProfileString(section, key, "", sb, sb.Capacity, filename)
        If res = 1 Then Return sb.ToString Else Return defaultValue
    End Function

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim filename As String = "C:\Scratch\Test.ini"
        CheckBox1.Checked = If(GetINIValue(filename, "Display", "bFXAAEnabled") = "1", True, False)
    End Sub

End Class

6 Comments

Thank you and others so much for your efforts to solve my annoying problem. One thing though, when i paste the code starting with "DLLImport", i get errors such as: "Type 'DllImport' is not defined.", "Type 'StringBuilder' is not defined.","Function 'GetPrivateProfileString' doesn't return a value on all code paths. Are you missing a 'Return' statement?
Yeah my bad, fixed it. OK that error is eliminated. Now there's this sucker: "'GetINIValue' is not declared. It may be inaccessible due to its protection level." It occures after pasting the third code in form, the one with checkbox. lol
yes i included your updated code but still not getting the checkbox checked. perhaps you can looks what's missing in my source codes. I can e-mail to you you if you wish.
So what gets returned from GetINIValue? Try debugging from there
If (GetINIValue(filename, "Display", "bFXAAEnabled") = "1", True, False) then always "False" gets returned.If; (GetINIValue(filename, "Display", "bFXAAEnabled") = "0", True, False) then always "True" gets returned. Doesn't matter what bFXAAEnabled= is in the ini file.
|
0
    'remember to import this at the top of your class
    Imports System.IO


    Dim filename As String = "C:\file.txt"
    Dim parts() As String = Nothing

    'use a stream reader to read the file line by line
    Using sr As New StreamReader(filename)
        'read a line as split into parts at the equal sign
        parts = sr.ReadLine.Split("="c)
        'check we actually have read the data in the correct format
        If parts.Length >= 2 Then
            Select Case parts(0)
                'use a case statement for the left hand side
                Case "valuexyz"
                    'now use a case statement for the right hand side
                    Select Case parts(1).Trim
                        Case "0"
                            Foo()
                        Case "1"
                            Bar()
                    End Select

                Case "valueabc"
                    Select Case parts(1).Trim
                        Case "0"
                            Foo2()
                        Case "1"
                            Bar2()
                    End Select
            End Select
        End If
    End Using

5 Comments

No luck? - Can you give an idea of what's wrong? Are you getting an error message?
No, i don't get any errors. The thing is, the code doesn't do anything. Let me show you my VB.net code screen: postimage.org/image/b5xsq83e3/5fde8101 This program i'm making needs to read options of Skyrim, a video game (which stores its settings in an .ini file) and then the program has to reflect those settings on checkboxes. if a value was set "1" in that ini file, then my program needs to read that value and set the checked state of a checkbox "true".
With your code written, the checkbox always remains unchecked whether the value was set "1" or "0" in the .ini file.
Can you post a section of the INI file into your original question?
I tested your code with an ordinary text file and it works perfectly fine. But it fais to work when it comes to that particular ini file. Odd indeed.
0

In your bottom case statement you are setting Checkbox8.checked property to false in either evetuallity. Decide in which circumstance you need to set the checkbox as checked and write the appropriate line to do that.

2 Comments

Is this supposed to be a comment on another answer?
Yes this was supposed to be in response to the comment - "With your code written, the checkbox always remains unchecked whether the value was set "1" or "0" in the .ini file.". I couldn't see how to directly respond to the comment. Is there a way to do this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.