|
-
Sep 21st, 2010, 11:27 AM
#1
Thread Starter
Member
Deleting a txt file
For example I have a open file dialog, which opens a txt file.
When I press a specific button, it clears the whole text file.
How do I do that?
-
Sep 21st, 2010, 11:34 AM
#2
Re: Deleting a txt file
What application do you open the text file with? If it's displayed in a textbox or richtextbox in your own application then you just clear the textbox (or richtextbox).
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Sep 22nd, 2010, 04:12 AM
#3
Thread Starter
Member
Re: Deleting a txt file
I did not show any of the content from the text file in any controls. The result is that the text file is now blank.
-
Sep 22nd, 2010, 05:10 AM
#4
Fanatic Member
Re: Deleting a txt file
So you don't actually want to delete the whole file, just remove it's contents?
vb.net Code:
Dim OFD As New OpenFileDialog OFD.Filter = ".txt Files (*.txt)|*.txt" 'might have done the filter wrong, i always forget :P If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then Dim finfo As New IO.FileInfo(OFD.FileName) If finfo.Extension.ToLower = ".txt" Then IO.File.Delete(OFD.FileName) IO.File.Create(OFD.FileName) End If End If
There are numerous ways to do this, this one just deletes the existing file and creates a new one in place of it. You could also use streamreader set on "append - False" and write nothing to the file or whatever you want really.
-
Sep 22nd, 2010, 05:58 AM
#5
Thread Starter
Member
Re: Deleting a txt file
Will I be able to write that file afterwards?
Im afraid the error "file being used by other" will show up.
-
Sep 22nd, 2010, 06:01 AM
#6
Fanatic Member
Re: Deleting a txt file
yes you will be able to write to the file, just try it out and see!
-
Sep 22nd, 2010, 06:12 AM
#7
Thread Starter
Member
Re: Deleting a txt file
Code:
Dim OFD As New OpenFileDialog
OFD.Filter = ".txt Files (*.txt)|*.txt"
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim finfo As New IO.FileInfo(OFD.FileName)
If finfo.Extension.ToLower = ".txt" Then
IO.File.Delete(OFD.FileName)
IO.File.Create(OFD.FileName)
End If
End If
Dim Write As New System.IO.Streamwriter(OFD.Filename)
Write.WriteLine("Test Line")
I typed that in and it gave me the "file is being used in other process" error.
-
Sep 22nd, 2010, 06:17 AM
#8
Hyperactive Member
Re: Deleting a txt file
Code:
Dim Write As New System.IO.Streamwriter(OFD.Filename)
Write.WriteLine("Test Line")
Write.Close()
Dont forget to close the stream!
-
Sep 22nd, 2010, 06:23 AM
#9
Thread Starter
Member
Re: Deleting a txt file
ah ok it seems I've resolved the question.
Thanks guys!
-
Sep 22nd, 2010, 06:32 AM
#10
Fanatic Member
Re: Deleting a txt file
Code:
Dim OFD As New OpenFileDialog
OFD.Filter = ".txt Files (*.txt)|*.txt"
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim finfo As New IO.FileInfo(OFD.FileName)
If finfo.Extension.ToLower = ".txt" Then
IO.File.Delete(OFD.FileName)
IO.File.Create(OFD.FileName)
Using sWrite As New IO.Streamwriter(OFD.Filename)
sWrite.WriteLine("Test Line")
End Using
End If
End If
Try that out, I changed up some stuff and moved your streamwriter argument. Are you sure you dont have the txt file open somewhere at the time you're trying to delete it?
-
Sep 22nd, 2010, 06:43 AM
#11
Hyperactive Member
Re: Deleting a txt file
Ps: that is too complicated 
Here I simplified a little so that you can learn from it:
Code:
Dim OFD As New OpenFileDialog
OFD.Filter = ".txt Files (*.txt)|*.txt"
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim finfo As New IO.FileInfo(OFD.FileName)
If finfo.Extension.ToLower = ".txt" Then
'If File exists, delete it.
If FileIO.FileSystem.FileExists(OFD.FileName) = True Then
Kill(OFD.FileName)
End If
'Write your data
Dim Write As New System.IO.StreamWriter(OFD.FileName)
Write.WriteLine("Test Line")
Write.Close()
End If
End If
-
Sep 22nd, 2010, 06:46 AM
#12
Fanatic Member
Re: Deleting a txt file
Okay fixed it so much easier!
vb.net Code:
Dim OFD As New OpenFileDialog OFD.Filter = ".txt Files (*.txt)|*.txt" If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then Dim fInfo As New IO.FileInfo(OFD.FileName) If fInfo.Extension.ToLower = ".txt" Then Dim fStream As New IO.FileStream(OFD.FileName, IO.FileMode.Truncate) fStream.Close() Using sWrite As New IO.StreamWriter(OFD.FileName) sWrite.WriteLine("Test Line") End Using End If End If
Oh yeah, if what you're trying to do is clear the text file so that you can start adding lines from scratch, the simplest way to do this is very simple:
vb.net Code:
Dim OFD As New OpenFileDialog OFD.Filter = ".txt Files (*.txt)|*.txt" If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then Dim fInfo As New IO.FileInfo(OFD.FileName) If fInfo.Extension.ToLower = ".txt" Then Using sWrite As New IO.StreamWriter(OFD.FileName, False) '//write here End Using End If End If
Last edited by J-Deezy; Sep 22nd, 2010 at 06:49 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|