无法正确读取文本文件

时间:2019-08-01 14:34:50

标签: vb.net

当我阅读txt文件并添加到ListBox1.items中时,我想要添加此文本http://prntscr.com/on12e0正确的文本§eUltra§8[§716x§8] .zip,而不是这样的http://prntscr.com/on11kv

我的代码

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
    My.Computer.FileSystem.CopyFile(
    appDataFolder & "\.minecraft\logs\latest.log",
    appDataFolder & "\.minecraft\logs\latestc.log")

    Using reader As New StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\.minecraft\logs\latestc.log")


        While Not reader.EndOfStream
            Dim line As String = reader.ReadLine()
            If line.Contains(" Reloading ResourceManager: Default,") Then
                Dim lastpart As String = line.Substring(line.LastIndexOf(", ") + 1)

                ListBox1.Items.Add(lastpart)
            End If
        End While
    End Using
    My.Computer.FileSystem.DeleteFile(appDataFolder & "\.minecraft\logs\latestc.log")
End Sub

1 个答案:

答案 0 :(得分:0)

此问题仅与您的第一个问题不同,因为您已用ListBox代替RichTextBox。看来您对第一个问题的回答完全可以接受。但我会再试一次。

首先获取文件的路径。我不知道您为什么要复制文件,所以我没有。

在文件顶部添加Imports System.IO。您可以使用File类方法。 memcpy返回一个字符串数组。

接下来使用Linq获取所需的物品。不要在每次循环迭代时更新用户界面。不可见的Linq循环只是将项目添加到数组中。然后,您可以使用File.ReadAllLines更新一次UI。

.AddRange

如果此答案和您之前的两个答案不起作用,请告诉我们原因。