public static void WriteToCsv(string filePath, int targetRow, int targetColumn, string newData)
{
List<List<string>> table = new List<List<string>>();
// Read all lines from the file.
var lines = File.ReadAllLines(filePath);
foreach (var line in lines)
{
// Split each line into columns based on comma delimiter.
var columns = line.Split(',');
table.Add(new List<string>(columns));
}
// Ensure the target row exists; add empty rows if necessary.
while (table.Count <= targetRow)
{
table.Add(new List<string>());
}
// Ensure enough columns exist in the target row.
while (targetColumn >= table[targetRow].Count)
{
table[targetRow].Add("");
}
// Modify the cell at the specified position with new data.
table[targetRow][targetColumn] = newData;
// Convert back to a single string per line and save changes.
var updatedContent = "";
foreach (var row in table)
{
updatedContent += String.Join(",", row.ToArray()) + Environment.NewLine;
}
File.WriteAllText(filePath, updatedContent);
}
07-17
652
