将文本行加载到变量
loading lines of text to variables
有这个文本文件,一个 .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
所以我可以把它写成这种形式
加载后,有没有办法拆分文本,因为我只需要值,例如:max-tick-time=60000
我只需要 60000
.
这可能吗?如果可以,我该怎么做?
您可以使用 System.IO.File.ReadAllLines. You can split the keys from the values using the String.Split 函数轻松读取文件中的每一行。
我建议将所有 key/value 对放在一个 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")
希望对你有用!
如果您不知道某些代码的作用,请告诉我,我会解释
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")
有这个文本文件,一个 .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
所以我可以把它写成这种形式
加载后,有没有办法拆分文本,因为我只需要值,例如:max-tick-time=60000
我只需要 60000
.
这可能吗?如果可以,我该怎么做?
您可以使用 System.IO.File.ReadAllLines. You can split the keys from the values using the String.Split 函数轻松读取文件中的每一行。
我建议将所有 key/value 对放在一个 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")
希望对你有用! 如果您不知道某些代码的作用,请告诉我,我会解释
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")