Kojof
Kojof

Reputation: 439

Update or Delete a Line from CSV using C#

can anyone please help me with this query.

I have a CSV file which is used by a unix application as a config file.

The file has no header and the first column is unique.

This is the format of the file.

XXX1,11112009,11112009
XXX2,11112009,11112009

How do i, using C#:

1) update a a whole line
2) Delete a line

I tried using OLEDB to update the file, but because there is no header,it's difficult to work with.

regards

K

Upvotes: 2

Views: 4493

Answers (4)

rf_circ
rf_circ

Reputation: 1915

When you need to update CSV files, pretty much the only answer is to read the whole thing in, modify it and write it out again. Or if it's large, you interleave those operations one line at a time.

Upvotes: 1

Robert Massa
Robert Massa

Reputation: 4608

I would recommend the FileHelpers library, you can make simple classes which map to you file structure. This way you can have a strongly-typed representation of the file which is easy to edit and save again.

It's pretty fast too.

Upvotes: 0

cjk
cjk

Reputation: 46415

Read the file lines into a String[] using File.ReadAllLines(path) then loop through and output as necessary.

This will work up to a few hundred thousand lines.

Upvotes: 2

Deepak
Deepak

Reputation: 7917

If you don't have header then you can use "Microsoft.Office.Interop.Excel" DLL. To get the reference of this dll, use "Add Reference" and ".NET" tab.

You can you it to check each field value by looping and can update and delete , which is matched.

Upvotes: -1

Related Questions