嗨 只是一个问题...
foreach (string file in files)
{
MessageBox.Show(String.Format("Name: {0}", file));
List<string> newColumnData = new List<string>() { file };
List<string> lines = File.ReadAllLines(file).ToList();
lines[1] += ",Name";
int index = 2;
//add new column value for each row.
lines.Skip(2).ToList().ForEach(line =>
{
//-1 for header
MessageBox.Show(index.ToString());
lines[index] += "," + newColumnData[index - 1];
index++;
});
//write the new content
File.WriteAllLines(file, lines);
}
数据看起来像:(标题有 2 行。)
7/1/2014 to 7/11/2014
Category Trans Date Post Date Merchant Name Amount
Dining Out 6/30/2014 7/1/2014 CLASSICS MARKET CAFE 8.41
Dining Out 7/1/2014 7/2/2014 CHIPOTLE 12.07
Dining Out 7/3/2014 7/6/2014 THAI CUISI 18.24
Groceries 7/2/2014 7/3/2014 FESTIVAL FOODS 12.79
我想添加一个新的列名示例“fileName”,值为“fileName”......只是为了方便。
我在以下位置收到相同的错误:
lines[index] += "," + newColumnData[index - 1];
但是通过调试器/消息框检查时,索引的值永远不会 <0。如何在第一个位置而不是最后一列添加新列?