簡體   English   中英

努力讀取txt文件中的選定文本

[英]Struggling with reading selected text from a txt file

我有一個名為reports.txt的文本文件,可以在Rich Text上打開它,但是有什么辦法可以打開它,但只顯示選定的文本

該文件包含

CPU Score: 322
CPU General: 708473
CPU Targeted: 341030088
RAM Score: 135
RAM Size (MB): 4001
RAM Speed (MB/s): 12144
GPU Compute (GFLOPS): 181
Disk Score: 49
Disk Write (MB/s): 117
Disk Read (MB/s): 322

例如,我想它顯示322, 135, 49在像一個TextBox

可能和簡單:

void Main()
{
    var dict = File.ReadAllLines(@"c:\yourFolder\yourFile.txt")
    .Select(f => f.Split(':'))
    .Select(f => new {
        Name=f[0].Trim(),
        Value=f[1].Trim()
    })
    .ToDictionary(f => f.Name, f=>f.Value);

    Console.WriteLine($"{dict["CPU Score"]}, {dict["RAM Score"]}, {dict["Disk Score"]}" );
}

謝謝Cetin Basoz

這是您可以幫助我的代碼。 我只需要把它加起來就可以得到總分:)

        private void novabnt_Click(object sender, EventArgs e)
    {
        var dict = File.ReadAllLines(@"c:\source\reports.txt")
        .Select(f => f.Split(':'))
        .Select(f => new
        {
            Name = f[0].Trim(),
            Value = f[1].Trim()
        })
        .ToDictionary(f => f.Name, f => f.Value);

        txtrams.Text = String.Empty;
        txtrams.AppendText(dict["RAM Score"]);
        txtCPUS.Text = String.Empty;
        txtCPUS.AppendText(dict["CPU Score"]);
        txthdds.Text = String.Empty;
        txthdds.AppendText(dict["Disk Score"]);
        txttotals.Text = String.Empty;

C#程序正在運行的照片

最后,我將讓它運行一個程序,該程序創建這些分數並更新文本文件,然后運行此命令。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM