delete a line from a textfile?

hannes

New member
Joined
Jan 11, 2011
Messages
2
Programming Experience
3-5
Hi all,

I'm making a function that is supposed to delete a line from a textfile "test.txt".
I'm having no problems with finding what line i want to delete. The basic idea is this:
  • read the whole file with streamreader
  • delete the line
  • rewrite the file with streamwriter

My problem is the last, how can i completely remove the file content, and write
another string in it, writer.writeline just appends a line.

Hannes
 
The simplest way is to call IO.File.ReadAllLines to get a String array with one element per line. You can the create a List and load the array into that. You then Remove the item from the List. Finally, call ToArray and IO.File.WriteAllLines to write out the new content.
 
Back
Top