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?
Printable View
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?
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).
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.
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.
Will I be able to write that file afterwards?
Im afraid the error "file being used by other" will show up.
yes you will be able to write to the file, just try it out and see!
I typed that in and it gave me the "file is being used in other process" error. :confused: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")
Dont forget to close the stream! :)Code:Dim Write As New System.IO.Streamwriter(OFD.Filename)
Write.WriteLine("Test Line")
Write.Close()
ah ok it seems I've resolved the question.
Thanks guys!
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?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
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
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