Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I basically want a list view to read the contents of an inifile called mods.ini and it to be able to check or uncheck based on a + or - in the ini.

What I have tried:

The code below works as follows:
There is a folder in the applications directory called Profiles, inside of this there is another folder which is the name of the profile. finally, there is an ini file inside of this folder. This ini file is written like this :
[Mods]
+=extendedtimeline.zip
-=BetterGraphicalImprovementsMod.zip
+=RuleTheWaves.zip
-=edfae.zip



This is where the problems start.

As the ini file is loaded i want it to check or uncheck the incoming items as indicated by the ini file. (+ means checked, - means unchecked)


Problem:
Now this code, works well it does indeed add each line to the list view however when it comes time to check or uncheck:
VB
ListView1.Items.Item(0).Checked = True
messes it up. This code will only check the first item in the list (because of the .Item(0)) so how would i go about doing it for every item in the list?



VB
Public Sub LoadMods()
    ListView1.Clear()
    Try
        Dim objReader As New System.IO.StreamReader(My.Application.Info.DirectoryPath + "/profiles/" + ListBox1.SelectedItem.ToString + "/mods.ini", System.Text.Encoding.Default)
        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 enabledyes = data(0)
                Dim path = data(1)

                Debug.Print(Name & "=" & path)
                ListView1.Items.Add(path) '& "=" & value
                If enabledyes = "+" Then
                    ListView1.Items.Item(0).Checked = True
                End If
            Else
                match = False
            End If


        Loop
        objReader.Close()
    Catch ex As Exception

    End Try


End Sub
Posted
Updated 1-Sep-17 16:03pm
v5

1 solution

Why Ini file? JSON[^] is so much easier these days...

Also, it has been a while since I have worked with INI files but, acording to the INI file specification - Wikipedia[^], you can't use multiples of the same key. So this:
[Mods]
+=extendedtimeline.zip
-=BetterGraphicalImprovementsMod.zip
+=RuleTheWaves.zip
-=edfae.zip

Needs to be changed to something like:
[Mods]
file1=extendedtimeline.zip,+
file2=BetterGraphicalImprovementsMod.zip,-
file3=RuleTheWaves.zip,+
file4=edfae.zip,-

If you do choose to work with JSON, this article has a JsonHelper class that will convert to/from class/text(file): Working with JSON in C# & VB[^]

UPDATE: Here is what it would look like if you changed to JSON and used the helper class from the link above:

1. Config file:
{
    "Mods": {
        "Files": [{
            "Name": "extendedtimeline.zip",
            "IsActive": true
        },
        {
            "Name": "BetterGraphicalImprovementsMod.zip",
            "IsActive": false
        },
        {
            "Name": "RuleTheWaves.zip",
            "IsActive": true
        },
        {
            "Name": "edfae.zip",
            "IsActive": false
        }]
    }
}

2. As a class structure:
VB
Class Settings
    Public Property Mods As ModFiles
    ' other sections go here....
End Class

Class ModFiles
    Public Property Files As List(Of ModFile)
End Class

Class ModFile
    Public Property Name As String
    Public Property IsActive As Boolean
End Class

3. And the code to read/write the config file:
VB
Imports Newtonsoft.Json

Module Module1

    Private settingsFilename As String = "Settings.Json"
    Private ReadOnly appPath As String = Environment.CurrentDirectory
    Private ReadOnly fullPath As String = IO.Path.Combine(appPath, settingsFilename)

    Private Property AppSettings() As Settings

    Sub Main()

        If IO.File.Exists(fullPath) Then
            Dim rawJson = IO.File.ReadAllText(fullPath)
            AppSettings = JsonHelper.ToClass(Of Settings)(rawJson)
        Else
            AppSettings = New Settings() With {
                    .Mods = New ModFiles() With {
                        .Files = New List(Of ModFile)() From {
                            New ModFile() With {
                                .Name = "extendedtimeline.zip",
                                .IsActive = True},
                            New ModFile() With {
                                .Name = "BetterGraphicalImprovementsMod.zip"},
                            New ModFile() With {
                                .Name = "RuleTheWaves.zip",
                                .IsActive = True},
                            New ModFile() With {
                                .Name = "edfae.zip"}
                        }
                    }
                }

            Dim rawJson = JsonHelper.FromClass(AppSettings)
            IO.File.WriteAllText(fullPath, rawJson)
        End If

        ' check values are read from file using IntelliSense or locals debug window
        Debugger.Break() 

    End Sub

End Module
 
Share this answer
 
v3

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