我想将带有标题的列表保存到csv文件中,然后再读回。
但是我需要DateTimes来显示毫秒数。
我可以通过更改区域性来写它。但是我在读取时遇到了缺少的Header错误。
private class TradeData
{
public string Symbol { get; set; }
public DateTime utcDT { get; set; }
public int SignedAmount { get; set; }
public double MidPrice { get; set; }
public double BidPrice { get; set; }
public double AskPrice { get; set; }
}
private static List<TradeData> Trades = new List<TradeData>();
public static void WriteIt()
{
using (var writer = new StreamWriter(Path.Combine(jobDirectoryPath, runName, "TickTraderSim7.csv")))
using (var csv = new CsvWriter(writer))
{
var myCI = CultureInfo.GetCultureInfo("en-US");
CultureInfo myCIclone = (CultureInfo)myCI.Clone();
myCIclone.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
myCIclone.DateTimeFormat.LongTimePattern = "HH:mm:ss.ffffff";
csv.Configuration.CultureInfo = myCIclone;
csv.WriteRecords(Trades);
}
}
public static void ReadIt()
{
using (var reader = new StreamReader(Path.Combine(jobDirectoryPath, runName, "TickTraderSim7.csv")))
using (var csv = new CsvReader(reader))
{
var myCI = CultureInfo.GetCultureInfo("en-US");
CultureInfo myCIclone = (CultureInfo)myCI.Clone();
myCIclone.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
myCIclone.DateTimeFormat.LongTimePattern = "HH:mm:ss.ffffff";
csv.Configuration.CultureInfo = myCIclone;
var x = csv.GetRecords<TradeData>();
Trades = x.Select(f => (TradeData)f).ToList();
}
}