使用C#从txt文件读取选定的txt时苦苦挣扎 [英] Struggling with reading selected txt from a txt file using C#
问题描述
我有一个名为reports.txt的文本文件,我可以在Rich Text上打开但是无论如何我可以打开但只显示所选文本
该文件包含的CPU分数:322 CPU通用:708473 CPU目标:341030088 RAM得分:135 RAM大小(MB):4001 RAM速度(MB / s):12144 GPU计算(GFLOPS):181磁盘分数: 49磁盘写入(MB / s):117磁盘读取(MB / s):322
例如我希望它显示322,135,49之类似一个文本框。你能做到这一点,任何人都可以帮助我。
我尝试过:
我尝试过富文本但我似乎只能打开整个txt文件。
I have a text file called reports.txt that I am able to get to open on Rich Text but is there anyway that I can get it to open but only display selected text
the file contains 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
for example i would like it to display 322, 135, 49 in like a text box. You are able to do this would anyone be able to help me please.
What I have tried:
I have tryed rich text but i only seem to be able to open the whole txt file.
推荐答案
你可以试试一个正则表达式:
You could try a regex:
(?<=Score:\s+)\d+(?=\s+)
应该这样做
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"]);
这篇关于使用C#从txt文件读取选定的txt时苦苦挣扎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!