-2

I have a .txt file with text in the following format:

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:

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; }
}
mohrjon1
  • 19
  • 1
  • 1
  • 4
  • I think about something to split it can I do this? – mohrjon1 Jul 24 '14 at 19:43
  • 1
    Welcome to stackoverflow, please spare a minute to read the [help center](http://stackoverflow.com/help/how-to-ask) – Yuliam Chandra Jul 24 '14 at 19:47
  • about the splitting, you can read this [similar post](http://stackoverflow.com/questions/14407762/c-sharp-split-textfile-into-a-2-dimensional-string-array) that delimited by comma instead of tab in this case – Yuliam Chandra Jul 24 '14 at 19:51

2 Answers2

6

There are a couple tasks involved in this:

  1. Read the text file into objects
  2. Display those objects in a 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:

<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 ProcessInfo class. 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.

kara
  • 3,205
  • 4
  • 20
  • 34
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • @mohrjon1 I noticed you unaccepted my answer. Was there something incorrect that I could fix? – BradleyDotNET Jul 29 '14 at 16:14
  • No thank you for your answer. I can now split the Text by Spaces but how I can add it to a new Column? – mohrjon1 Jul 30 '14 at 08:32
  • @mohrjon1 Since you have it split, you should be able to parse it into an object. Then add a column like the one in my sample bound to whichever properties you want. Could you clarify which step is confusing you? – BradleyDotNET Jul 30 '14 at 16:24
  • I wan`t to split it with this Code: RegexOptions options = RegexOptions.None; Regex regex = new Regex(@"[ ]{2,}", options); string fs = regex.Replace(s, @" "); var fs1 = fs.Split(' ','\n'); f but I dont know how I can add from these Columns – mohrjon1 Jul 31 '14 at 21:52
2

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];
...
Miulx
  • 61
  • 4