Search code examples
c#wpflistviewgridviewcolumn

C# WPF read a .txt file line by line and store it to listview with GridViewColumn


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; }
}

Solution

  • 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.