470,596 Members | 1,438 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

Clear the content of a file

Hello.
How do you clear the content of a *.txt file?
Thanks.

--
Xero

http://www.chezjeff.net
My personal web portal
Nov 21 '05 #1
4 7023
"Xero" <jeff_@_chezjeff_._net(remove_underscores_and_this )> wrote in message
news:43**********************************@microsof t.com...
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
Nov 21 '05 #2
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" <jeff_@_chezjeff_._net(remove_underscores_and_this )>
Hello.
How do you clear the content of a *.txt file?
Thanks.

Nov 21 '05 #3
I see.
Thanks very much, guys.

Xero
"Cor Ligthert" wrote:
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" <jeff_@_chezjeff_._net(remove_underscores_and_this )>
Hello.
How do you clear the content of a *.txt file?
Thanks.


Nov 21 '05 #4
"Xero" <jeff_@_chezjeff_._net(remove_underscores_and_this )> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jan | last post: by
2 posts views Thread by Sure | last post: by
4 posts views Thread by Christian Otteneuer | last post: by
3 posts views Thread by Kyle Kolander | last post: by
4 posts views Thread by aikwee | last post: by
4 posts views Thread by Dhans | last post: by
3 posts views Thread by DHarry | last post: by
3 posts views Thread by ffrugone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.