Clear the content of a file

  • Thread starter Thread starter Guest
  • Start date Start date
Xero said:
How do you clear the content of a *.txt file?

System.IO.File.Open( "foo.xml", System.IO.FileMode.Truncate).Close

Bit of an audacious one-liner, but you get the drift. Open the file in truncate
mode, then (as there's nothing really to do in truncate mode after having reset
the file to zero-length) close it.

Opening it with FileMode.Create will also achieve the same thing, and allow
you to use the FileStream afterwards, for instance,

Dim fileStream As System.IO.FileStream
fileStream = System.IO.File.Open( "foo.xml", System.IO.FileMode.Create)
'
' foo.xml is now zero-length, plus use can write to fileStream.
'
fileStream.Flush
fileStream.Close


Derek Harmon
 
Jeff,

Derek gave you the answer, however what he did not write in my opinon in
words was.

"Just create an empty one with the same name". (And than use one of his
samples)

Only meant as addition

Cor
 
I see.
Thanks very much, guys.

Xero
Cor Ligthert said:
Jeff,

Derek gave you the answer, however what he did not write in my opinon in
words was.

"Just create an empty one with the same name". (And than use one of his
samples)

Only meant as addition

Cor
 
Xero said:
How do you clear the content of a *.txt file?

Open the file using 'FileStream' and use its 'SetLength' method to reset the
file's length.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top