Hi,
have a look at this runnable example. feel free to ask any questions...
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace ReadCSVToDictionary
{
class Program
{
class LineInformation
{
public string Key { get; set; }
public string StringValue { get; set; }
public int IntValue { get; set; }
public DateTime DateTimeValue { get; set; }
}
static void Main(string[] args)
{
string strPathToCSVFile = @"myfile.csv";
#region Create example file
List<lineinformation> listExample = new List<lineinformation>();
listExample.Add(new LineInformation { Key = "value1", StringValue = "blabla", IntValue = 1, DateTimeValue = DateTime.Now });
listExample.Add(new LineInformation { Key = "value2", StringValue = "ahahah", IntValue = 44, DateTimeValue = DateTime.Now.AddHours(1) });
listExample.Add(new LineInformation { Key = "value3", StringValue = "what?", IntValue = 5519, DateTimeValue = DateTime.Now.AddDays(1) });
StringBuilder sbFileContent = new StringBuilder();
foreach (LineInformation li in listExample)
{
sbFileContent.AppendFormat("{0};{1};{2};{3}" + Environment.NewLine, li.Key, li.StringValue, li.IntValue, li.DateTimeValue);
}
File.WriteAllText(strPathToCSVFile, sbFileContent.ToString());
#endregion // Create example file
string[] astrLines = File.ReadAllLines(strPathToCSVFile);
const char chSeparator = ';';
Dictionary<string,> dict = new Dictionary<string,>();
foreach (string strLine in astrLines)
{
string[] astrSplit = strLine.Split(chSeparator);
LineInformation lineinformation = new LineInformation
{
Key = astrSplit[0],
StringValue = astrSplit[1],
IntValue = Convert.ToInt32(astrSplit[2]),
DateTimeValue = DateTime.Parse(astrSplit[3])
};
dict.Add(lineinformation.Key, lineinformation);
}
foreach(KeyValuePair<string,> kvp in dict)
{
Console.WriteLine("{0};{1};{2};{3}", kvp.Value.Key, kvp.Value.StringValue, kvp.Value.IntValue, kvp.Value.DateTimeValue);
}
Console.ReadKey();
}
}
}