Reputation: 439
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
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
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
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
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