C# WPF 逐行读取 .txt 文件并使用 GridViewColumn 将其存储到列表视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24942568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# WPF read a .txt file line by line and store it to listview with GridViewColumn
提问by mohrjon1
I have a .txt file with text in the following format:
我有一个带有以下格式文本的 .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
I have removed the Spaces to one Space and want to split the File with this to add evrything under Name Pid and so on to a GridviewColumn every line is a extra line in the GridViewColumn how I can solve this to add evry line to a GridViewColumn:
我已将 Spaces 删除到一个 Space,并想用这个拆分文件以将 Name Pid 下的所有内容添加到 GridviewColumn 中,每一行都是 GridViewColumn 中的一个额外行,我如何解决这个问题以将 evry 行添加到 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; }
}
回答by BradleyDotNET
There are a couple tasks involved in this:
这涉及到几个任务:
- Read the text file into objects
- Display those objects in a
GridView
- 将文本文件读入对象
- 将这些对象显示在
GridView
The first isn't too bad. First we need a class to store everything in:
第一个还不错。首先,我们需要一个类来存储所有内容:
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; }
}
Then we need to parse the text file:
然后我们需要解析文本文件:
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);
}
}
Finally, we need to set up the XAML:
最后,我们需要设置 XAML:
<ListView ItemsSource="{Binding Processes}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"/>
...
</GridView>
</ListView.View>
</ListView>
Bind each column to a property in the ProcessInfoclass. I included a sample for the first column. Of course, you need to expose the parsed collection as a public property of your View Model for this to work.
将每一列绑定到ProcessInfo类中的一个属性。我在第一列中包含了一个示例。当然,您需要将解析后的集合公开为您的视图模型的公共属性才能使其工作。
回答by Miulx
For spliting you can use regex.
对于拆分,您可以使用正则表达式。
e.g.
例如
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];
...

