我已經實現了一些代碼,以便對我的csv文件進行更改。但是,每次運行該程序時,我都會遇到一個IndexOutOfRangeException
錯誤。IndexOutOfRangeException未處理警告
class Program
{
static void Main(string[] args)
{
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "kaviaReport 02_08_2016.csv");
var fileContents = ReadFile(filePath);
foreach (var line in fileContents)
{
Console.WriteLine(line);
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
public static IList<string> ReadFile(string fileName)
{
var results = new List<string>();
int lineCounter = 0;
string currentLine = string.Empty;
var target = File.ReadAllLines(fileName);
//.Skip(1) // Skip the line with column names
while ((currentLine = fileName) != null)//while there are lines to read
{
if (lineCounter != 0)
{
//If it's not the first line
var lineElements = currentLine.Split(',');//split your fields into an array
lineElements[4] = lineElements[4].Replace(' ', ',');//replace the space in position 4(field 5) of your array
//target.WriteAllLines(string.Join(",", fielded));//write the line in the new file
File.WriteAllLines(fileName, target);
}
lineCounter++;
}
return results;
}
}
}
請修改你的問題一點,不要讓我們做所有的工作。 請參閱[mcve](http://stackoverflow.com/help/mcve) –
答案有助於解決錯誤嗎? – Marusyk