Я внедрил некоторый код, чтобы внести изменения в мой файл 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