user1918410
user1918410

Reputation: 69

How to read a text file

I read text file and displayed in DataGridView data is displaying, but in the above text file is related to log files, I want to display that log file by month wise, without using a DataTable, if possible.

private void BtnUser_Click(object sender, EventArgs e)
{
  dgv1.Columns.Add("col1", "Ipaddress");
  dgv1.Columns.Add("col2", "Sysname");
  dgv1.Columns.Add("col3", "username");
  dgv1.Columns.Add("col4", "text");
  dgv1.Columns.Add("col5", "datetime");

  string line;
  StreamReader strRead = new StreamReader("D:\\login.lgl");
  {
    int row = 0;

    while ((line = strRead.ReadLine()) != null)
    {
      string[] columns = line.Split('|');
      dgv1.Rows.Add();
      for (int i = 0; i < columns.Length; i++)
      {
        dgv1[i, row].Value = columns[i];
      }
      row++;
    }
  }
}

Upvotes: 0

Views: 672

Answers (2)

SWeko
SWeko

Reputation: 30912

I would recommend that you create a class for the structure you are parsing in from the file, something like:

public class LogFileItem
{
   public string IpAddress {get; set;}
   public string Sysname {get; set;}
   public string Username {get; set;}
   public string Text {get; set;}
   public DateTime DateTime {get; set;}

   public static List<LogFileItem> ParseLogFile(string path)
   {
     List<LogFileItem> result = new List<LogFileItem>();

     //in a real scenario, this code should have a lot more error checks
     string line;
     StreamReader strRead = new StreamReader(path);

     while ((line = strRead.ReadLine()) != null)
     {
       string[] columns = line.Split('|');

       LogFileItem item = new LogFileItem();
       item.IpAddress = columns[0];
       item.Sysname = columns[1];
       item.Username = columns[2];
       item.Text = columns[3];
       //this assumes that the dateTime column is parsable by .net
       item.DateTime = DateTime.Parse(columns[4]);

       result.add(item); 
     }
     return result;
   }

}

then you could just do:

private void BtnUser_Click(object sender, EventArgs e)
{
   List<LogFileItem> logItems = LogFileItem.ParseLogFile(@"D:\login.lgl");
   dgv1.DataSource = logItems;
}

to display the data. Also you could filter the data any which way you want, and if you have a month/year pair to filter on, you could just do:

   List<LogFileItem> logItems = LogFileItem.ParseLogFile(@"D:\login.lgl");
   var logsPerMonth = logItems.Where(li => li.DateTime.Year = year && li.DateTime.Month == month);

Note that datetime parsing is somewhat of a dark art, so you could take a look at DateTime.ParseExact to make that work. Also, take a look at using an using statement, or reading the lines from a text file with File.ReadAllLines.

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460278

You could use Linq to group by month:

var logMonthGroups = File.ReadLines("D:\\login.lgl")
    .Select(l => new { Cols = l.Split('|') })
    .Select(x => new
    {
        Ipaddress = x.Cols.ElementAtOrDefault(0),
        Sysname = x.Cols.ElementAtOrDefault(1),
        username = x.Cols.ElementAtOrDefault(2),
        text = x.Cols.ElementAtOrDefault(3),
        datetime = x.Cols.ElementAtOrDefault(4) == null ? DateTime.MinValue : DateTime.Parse(x.Cols[4])
    })
    .GroupBy(x => new { Year = x.datetime.Year, Month = x.datetime.Month })
    .OrderBy(g => g.Key.Year).ThenBy(g => g.Key.Month);

foreach(var group in logMonthGroups)
{
    // add to the DataGridView ...
}

Upvotes: 1

Related Questions