Vb.net 读取txt并在richtexbox中添加

Vb.net 读取txt并在richtexbox中添加,vb.net,Vb.net,此代码 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load My.Computer.FileSystem.CopyFile( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\.minecraft\logs\late

此代码

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    My.Computer.FileSystem.CopyFile(
        Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\.minecraft\logs\latest.log",
        Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\.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("Setting user: ") Then
                    Dim lastpart As String = line.Substring(line.LastIndexOf(": ") + 1)
                    FlatAlertBox3.Text = lastpart
                    Exit While
                End If
            End While
        End Using
        My.Computer.FileSystem.DeleteFile(
            Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\.minecraft\logs\latestc.log")

    End Sub

我想在richtextbox1.text-like-up代码中添加这个名字,但它不起作用,它只添加名字而不是所有的名字,我想在richtextbox1-up代码中添加所有的名字,我该如何做plzz-help-me^ ^ ^

正如注释中建议的那样,您需要将
lastpart
附加到文本框的内容中,并在时移除
Exit,这将强制在第一次迭代时退出循环

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    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")
        ' Set text box to empty string.
        FlatAlertBox3.Text = ""
        While Not reader.EndOfStream
            Dim line As String = reader.ReadLine()
            If line.Contains("Setting user: ") Then
                Dim lastpart As String = line.Substring(line.LastIndexOf(": ") + 1)
                ' Append to contents of text box.
                FlatAlertBox3.Text += lastpart
                'Exit While
            End If
        End While
    End Using
    My.Computer.FileSystem.DeleteFile(appDataFolder & "\.minecraft\logs\latestc.log")

End Sub
试试这个:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim AppData As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
    Dim logs As String = Path.Combine(AppData, ".minecraft\logs")
    Dim templog As String = Path.Combine(logs, "latestc.log")

    File.Copy(Path.Combine(logs, "latest.log"), templog, True)

    Dim settings = File.ReadLines(templog).
        Where(Function(line) line.Contains("Setting user: "))

    FlatAlertBox3.Text = String.Join(vbCrLf, settings)

    File.Delete(templog)
End Sub
确保使用文件顶部的
Imports System.IO
。我还可以添加一个
Try/Finally
,以确保
文件.Delete()
运行

为了好玩,我们可以把它写成一行:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load   
    FlatAlertBox3.Text = String.Join(vbCrLf, File.ReadLines(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ".minecraft\logs\latest.log")).Where(Function(line) line.Contains("Setting user: ")))  
End Sub

尽管我从未在生产中使用过这种不可读的代码。

首先,您要替换文本框中的文本,使其每次都被覆盖。您需要附加这些名称。-除此之外,还可以删除
Exit While
,因为这会在找到第一个条目后停止循环(因此您只能得到第一个名称)。而不是调用
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData
四次,调用一次并分配给字符串变量。然后将该变量与
Path.Combine()
一起使用。