I just wrote another example of C# 3 handling a comma-separated file. It handles a code-change log with time-stamped records, created using Excel. The user is prompted for a date range; my program then searches for and displays the records that are in that range.
Here is the main body of the program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace CSVParse2
{
class Program
{
static void Main(string[] args)
{
string fileName = @"C:\CSVExample2.csv";
StreamReader stream = new StreamReader(fileName);
List<CodeChange> data = new List<CodeChange>();
while (stream.EndOfStream == false)
{
string record = stream.ReadLine();
string[] recordParts = record.Split(',');
CodeChange obj = new CodeChange(recordParts);
data.Add(obj);
}
Console.Write("Enter starting date (m/d/yyyy): ");
string startString = Console.ReadLine();
Console.Write("Enter ending date (m/d/yyyy): ");
string endString = Console.ReadLine();
string[] startArray = startString.Split('/', '-');
string[] endArray = endString.Split('/', '-');
int startYear = Int32.Parse(startArray[2]);
int startMonth = Int32.Parse(startArray[0]);
int startDay = Int32.Parse(startArray[1]);
DateTime startDate = new DateTime(startYear, startMonth, startDay);
int endYear = Int32.Parse(endArray[2]);
int endMonth = Int32.Parse(endArray[0]);
int endDay = Int32.Parse(endArray[1]);
DateTime endDate = new DateTime(endYear, endMonth, endDay);
if (startDate > endDate)
{
Console.WriteLine("Invalid date range");
}
else
{
bool found = false;
var matches =
from d in data
where d.Date >= startDate && d.Date <= endDate
select d;
foreach (var m in matches)
{
found = true;
Console.WriteLine("Date: {0}", m.Date.ToString());
Console.WriteLine("Programmer: {0}", m.Programmer);
Console.WriteLine("Changed: {0}", m.ChangedItem);
Console.WriteLine("Notes: {0}", m.Notes);
Console.WriteLine("");
}
if (!found)
Console.WriteLine("Match not found");
}
}
}
}
As you can see, the code creates a collection of objects, then transfers the data from the CSV file to that collection. My class holds the record data as a series of public members for the sake of simplicity. Its code is fairly trivial.
Here is an example of my code running:
Enter starting date (m/d/yyyy): 1/1/2007
Enter ending date (m/d/yyyy): 2/1/2007
Date: 1/2/2007 8:00:00 AM
Programmer: Ross Albertson
Changed: changelog.csv
Notes: created code change log
Date: 1/2/2007 1:00:00 PM
Programmer: Michael Garza
Changed: employee.cs
Notes: "added ""reason for leaving"" field to class"
Date: 1/10/2007 9:00:00 AM
Programmer: Carol Franck
Changed: test system
Notes: installed Visual Studio 2008 on test system
Of course, the data is ficticious, but you get the idea. To reinforce the idea that I'm not pulling any tricks, here is my data:
1/2/07 8:00,Ross Albertson,changelog.csv,created code change log
1/2/07 13:00,Michael Garza,employee.cs,"added ""reason for leaving"" field to class"
1/10/07 9:00,Carol Franck,test system,installed Visual Studio 2008 on test system
2/1/07 8:00,Ross Albertson,employee2.cs,created employee class as an entity definition
2/5/07 12:30,Sarah Ferguson,EmployeeManager,converted SQL to LINQ
2/28/07 9:30,Michael Garza,Q*Bert,began working on game
I apologize if I just insulted anybody. I know someone in our group asked me about Excel-generated CSV files, so I hope I answered the question.
Ross
Lay a .NET over the world