c# WPF逐行读取.txt文件,并将其存储到listview中
本文关键字:存储 listview 逐行 WPF 读取 txt 文件 | 更新日期: 2023-09-27 18:14:56
我有一个.txt文件,文本格式如下:
14:04:43 29.07.2014 Process information for JONAS-PC:
Name Pid CPU Thd Hnd Priv CPU Time Elapsed Time
Idle 0 98 8 0 0 1008:29:34.706 148:49:00.765
chrome 5020 1 14 169 136008 0:00:46.332 0:06:03.319
chrome 5960 0 10 149 335704 0:19:09.992 21:07:14.101
pslist 7564 1 2 164 3324 0:00:00.218 0:00:01.236
wininit 640 0 3 85 2528 0:00:00.078 148:48:53.839
csrss 664 0 14 1091 13240 0:03:13.675 148:48:53.823
services 696 0 6 302 10912 0:00:33.243 148:48:53.776
lsass 716 0 8 959 9176 0:01:01.979 148:48:53.698
lsm 728 0 11 189 3560 0:00:00.577 148:48:53.698
winlogon 808 0 3 121 4532 0:00:00.171 148:48:53.308
svchost 896 0 11 414 8260 0:02:41.273 148:48:47.817
svchost 988 0 13 377 9064 0:03:21.896 148:48:47.739
atiesrxx 136 0 6 132 2276 0:00:00.015 148:48:47.723
svchost 580 0 20 578 25760 0:01:23.975 148:48:47.708
svchost 800 0 28 633 177208 0:37:12.592 148:48:47.692
svchost 912 0 27 847 22020 0:00:20.638 148:48:47.692
svchost 1048 0 38 1432 52952 0:01:33.834 148:48:47.677
UMVPFSrv 1076 0 3 75 1412 0:00:00.109 148:48:47.677
MSI86DA.tmp 1244 0 4 66 2532 0:00:00.015 148:48:47.474
atieclxx 1376 0 10 140 3352 0:00:00.218 148:48:47.443
svchost 1408 0 17 532 34716 0:00:40.139 148:48:47.427
spoolsv 1672 0 12 315 9544 0:00:00.218 148:48:47.045
svchost 1744 0 18 452 32556 0:01:45.175 148:48:46.985
armsvc 1888 0 4 85 1316 0:00:00.000 148:48:46.915
我已经将空格删除到一个空间,并希望将文件与此分开,以添加名称Pid等下的所有内容到GridviewColumn每一行都是GridviewColumn中的额外行,我如何解决这个问题以将每一行添加到GridviewColumn:
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@"[ ]{2,}", options);
string fs = regex.Replace(s, @" ");
var fs1 = fs.Split(' ',''n');
_GameCollection.Add(new GameData {
Name = fs1[18],
PID = fs1[19],
CPU = fs1[20],
Thd = fs1[21],
Hnd = fs1[22],
CpuTime = fs1[23],
ElapsedTime = fs1[24]
});
_GameCollection.Add(new GameData
{
});
}
public ObservableCollection<GameData> GameCollection
{ get { return _GameCollection; } }
}
public class GameData
{
public string Name { get; set; }
public string PID { get; set; }
public string CPU { get; set; }
public string Thd { get; set; }
public string Hnd { get; set; }
public string CpuTime { get; set; }
public string ElapsedTime { get; set; }
}
这里涉及到几个任务:
- 将文本文件读入对象
- 在
GridView
中显示这些对象
第一个还不错。首先,我们需要一个类来存储所有内容:
public class ProcessInfo
{
public string Name { get; set; }
public int PID { get; set; }
public int CPU { get; set; }
public int Thd { get; set; }
public int Hnd { get; set; }
public TimeSpan CpuTime { get; set; }
public TimeSpan ElapsedTime { get; set; }
}
然后我们需要解析文本文件:
List<ProcessInfo> processes = new List<ProcessInfo>();
using(StreamReader reader = new StreamReader("input.txt'))
{
reader.ReadLine(); //The headers don't matter!
string currentLine;
while (currentLine = reader.ReadLine() != null)
{
ProcessInfo newInfo = new ProcessInfo();
//Actual parsing left up to the reader; String.Split is your friend.
processes.Add(newInfo);
}
}
最后,我们需要设置XAML:
<ListView ItemsSource="{Binding Processes}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"/>
...
</GridView>
</ListView.View>
</ListView>
将每个列绑定到ProcessInfo
类中的一个属性。我在第一栏里放了一个样本。当然,您需要将解析后的集合公开为视图模型的公共属性。
可以使用正则表达式进行分割。
。
string inputLine = "Idle 0 99 8 0 0 203:18:16.647 26:02:53.315";
string[] splitList = Regex.Split (inputLine, "''s{1,30}");
string name = splitList [0];
string pid = splitList [1];
string CPU = splitList [2];
...