Joe McGorry
Joe McGorry

Reputation: 25

Is there a way to add data into a specific row c#

I have a csv file that has data like

1, FirstName, LastName, DOB

2, FirstName, LastName, DOB

3, FirstName, LastName, DOB

4, FirstName, LastName, DOB

I was wondering if I was able to replace say the 3rd row with 3, John, Smith, 01/12/1999. I already have this saved in a string I just need to be able to overwrite a specific row, in this case row 3. Thanks

Upvotes: 1

Views: 634

Answers (1)

MyBug18
MyBug18

Reputation: 2230

It seems what you want has (almost) nothing to do with csv, but it can be done with some string manipulation.

private string yourFilePath;

private void EditRow(int rowNum, string edit)
{
    string[] rowsArray = IO.File.ReadAllLines(yourFilePath);
    rowsArray[rowNum] = edit;
    IO.File.WriteAllLines(yourFilePath, rowsArray);
}

As I know, there is no way to edit specific row of the file without opening it. So the code above just opens it all, edit the specific row, and overwrites entire file.

See also: System.IO.File

Upvotes: 1

Related Questions