将文本行加载到变量

时间:2015-11-25 20:49:22

标签: vb.net file variables minecraft

有这个文本文件,一个.proporties文件,如果可能的话,我想在VB.NET中打开单个变量中的单独行。

这是一个来自Minecraft服务器的文件,如下所示:

#Minecraft server properties
#Sun Nov 08 18:01:24 CET 2015
spawn-protection=16
max-tick-time=60000
generator-settings=
force-gamemode=false
allow-nether=true
gamemode=0
broadcast-console-to-ops=true
enable-query=false
player-idle-timeout=0
difficulty=1
spawn-monsters=true
op-permission-level=4
resource-pack-hash=
announce-player-achievements=true
pvp=true
snooper-enabled=true
level-type=DEFAULT
hardcore=false
enable-command-block=false
max-players=20
network-compression-threshold=256
max-world-size=29999984
server-port=25565
server-ip=
spawn-npcs=true
allow-flight=false
level-name=world
view-distance=10
resource-pack=
spawn-animals=true
white-list=false
generate-structures=true
online-mode=true
max-build-height=256
level-seed=
motd=A Minecraft Server
enable-rcon=false

有没有办法可以在每行中加载每行的文本:

dim line1 as string = readline1
dim line2 as string = readline2
dim line3 as string = readline3

所以我可以把它放在这种形式

CLICK ON ME FOR THE IMAGE

当它被加载时,我是否可以分割文本,因为我只需要该值,例如:max-tick-time=60000我只需要60000

这可能吗?如果可以,我该怎么做?

2 个答案:

答案 0 :(得分:1)

您可以使用System.IO.File.ReadAllLines轻松读取文件中的每一行。您可以使用String.Split功能从值中拆分键。

我建议将所有键/值对放在Dictionary实例中,然后将它们分配给变量:

    'Variable that holds your list of settings.
    Dim lstSettings As New Dictionary(Of String, String)

    'Loop through each line.
    For Each strLine As String In IO.File.ReadAllLines("C:\Some\FilePath.txt")

        'Ignore empty lines and comments.
        If strLine.Length > 0 AndAlso strLine.Substring(0, 1) <> "#" Then

            'Split the Key from the Value.
            Dim lstKeyValue As List(Of String) = strLine.Split("=").ToList()

            'Some additional filtering of bad data.
            If lstKeyValue.Count = 2 Then

                'Add to the Dictionary instance.
                lstSettings.Add(lstKeyValue(0), lstKeyValue(1))

            End If

        End If

    Next

    'Now assign the values to your variables or UI.
    Dim bolForceGameMode As Boolean = CBool(lstSettings("force-gamemode"))
    Dim intViewDistance As Integer = CInt(lstSettings("view-distance"))
    'etc

    'Or directly manipulate the UI:
    'txtViewDistance.Text = lstSettings("view-distance")

答案 1 :(得分:1)

我希望它适合你! 如果您不知道某些代码的作用,请告诉我,我将解释

Public Class Form1
    Dim properties As String() = {}
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim text As String = My.Computer.FileSystem.ReadAllText("") 'You put the file location here
        Dim alltext As String() = text.Split(Environment.NewLine)
        For i = 2 To alltext.Length - 1
            Array.Resize(properties, properties.Length + 1)
            properties(properties.Length - 1) = alltext(i).Split("=")(0)
            Array.Resize(properties, properties.Length + 1)
            properties(properties.Length - 1) = alltext(i).Split("=")(1)
        Next
    End Sub

    Function setting(val As String)
        For i = 0 To properties.Length - 1
            If properties(i).Contains(val) Then
                Return properties(i + 1)
            End If
        Next
        Return "No results found."
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox(setting(TextBox1.Text)) 'You find the setting with this setting("max-tick-time")
    End Sub
End Class

你可以这样调用这个函数:

Dim allowFlight As String = setting("allow-flight")