如何按照问题中描述的方式设置我的ini文件阅读器?请帮忙? [英] How do I set up my ini file reader the way described in the problem? Please help?

查看:126
本文介绍了如何按照问题中描述的方式设置我的ini文件阅读器?请帮忙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想要一个列表视图来读取一个名为mods.ini的inifile的内容,它可以根据ini中的+或 - 来检查或取消选中。



我尝试过:



以下代码的工作原理如下:

有应用程序目录中名为Profiles的文件夹,其中有另一个文件夹,即配置文件的名称。最后,这个文件夹里面有一个ini文件。这个ini文件是这样写的:

 [Mods] 
+ = extendedtimeline.zip
- = BetterGraphicalImprovementsMod.zip
+ = RuleTheWaves.zip
- = edfae.zip







这是问题开始的地方。



当加载ini文件时,我希望它检查或取消选中ini文件所指示的传入项目。 (+表示已选中, - 表示未选中)





问题:

现在这段代码,效果很好确实将每一行添加到列表视图中,但是当需要检查或取消选中时:

 ListView1.Items.Item( 0 )。选中=  True  

搞砸了。这段代码只检查列表中的第一项(因为.Item(0))所以我该如何为列表中的每一项做这件事?







 公开  Sub  LoadMods()
ListView1.Clear()
尝试
Dim objReader As System.IO.StreamReader(My.Application.Info.DirectoryPath + / profiles / + ListBox1.SelectedItem.ToString + / mods.ini,System.Text.Encoding。默认
objReader.Peek()<> -1
row = objReader.ReadLine()
如果 LCase(row)= [& LCase(部分)& ] 然后
match = True
ElseIf 匹配 AndAlso 修剪(行)<> AndAlso Mid(row,< span class =code-digit> 1 , 1 )<> [ 然后
Dim data() As String = Split (row, =
Dim enabledyes = data( 0
Dim path = data( 1

Debug.Print(Name& =& path)
ListView1.Items.Add(path)' &安培; =&值
如果 enabledyes = + 然后
ListView1.Items.Item( 0 )。已检查= True
结束 如果
否则
match = False
结束 如果


循环
objReader.Close()
Catch ex As 异常

结束 尝试


结束 Sub

解决方案

为什么选择Ini文件? JSON [ ^ ]现在这么容易了......



此外,自从我使用INI文件以来已经有一段时间了,但是,根据 INI文件规范 - 维基百科 [ ^ ],您不能使用相同键的倍数。所以这个:

 [Mods] 
+ = extendedtimeline.zip
- = BetterGraphicalImprovementsMod.zip
+ = RuleTheWaves.zip
- = edfae.zip



需要更改为:

 [Mods] 
file1 = extendedtimeline.zip,+
file2 = BetterGraphicalImprovementsMod.zip, -
file3 = RuleTheWaves.zip,+
file4 = edfae.zip, -



如果您确实选择使用JSON,那么本文有一个JsonHelper类,它将转换为/从类/文本(文件):在C#中使用JSON& VB [ ^ ]



更新:如果您更改为JSON并使用链接中的帮助程序类,则会出现这种情况上述:



1.配置文件:

 {
Mods:{
文件:[{
名称:extendedtimeline.zip,
IsActive:true
},
{
名称: BetterGraphicalImprovementsMod.zip,
IsActive:false
},
{
Name:RuleTheWaves.zip,
IsActive:true
},
{
名称:edfae.zip,
IsActive:false
}]
}
}



2.作为班级结构:

 班级设置
公共 属性 Mods 作为 ModFiles
' 其他部分到这里....
结束

ModFiles
公众 属性文件作为列表( ModFile)
结束

ModFile
公共 属性名称作为 字符串
公共 属性 IsActive As 布尔
结束



3.读取/写入配置文件的代码:

  Imports  Newtonsoft.Json 

模块 Module1

私有 settingsFilename 作为 字符串 = Settings.Json
私人 ReadOnly appPath 作为 字符串 = Environment.CurrentDirectory
私有 ReadOnly fullPath 作为 字符串 = IO.Path.Combine(appPath,settingsFilename)

私有 属性 AppSettings()正如设置

Sub Main()

如果 IO.File.Exists(fullPath)那么
Dim rawJson = IO .File.ReadAllText(fullPath)
AppSettings = JsonHelper.ToClass( of 设置)(rawJson)
其他
AppSettings = 设置()使用 {
.Mods = ModFiles()使用 {
.Files = 列表( of ModFile)()来自{
ModFile()使用 {
.Name = extendedtimeline.zip
.IsActive = True },
ModFile()使用 {
.Name = BetterGraphicalImprovementsMod.zip},
ModFile( )使用 {
.Name = RuleTheWaves .zip
.IsActive = True },
新 ModFile()使用 {
.Name = edfae.zip}
}
}
}

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

' 使用IntelliSense或本地调试从文件中读取检查值窗口
Debugger.Break()

结束 Sub

结束 模块


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:

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?



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

解决方案

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:

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:

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


这篇关于如何按照问题中描述的方式设置我的ini文件阅读器?请帮忙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆