2

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

1
  • 1
    How static is the file, how many lines does it have 10, 100, 1,000,000 or 100,000,000? Size makes a difference. Commented Nov 11, 2009 at 13:14

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for all the responses, this one was the closet to what i used.
1

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.

Comments

0

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.

Comments

-1

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.

1 Comment

Unfortunately I cannot have this "Microsoft.Office.Interop.Excel" DLL running on the production server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.