Home and Learn: VB Net Course
This lesson is part of an ongoing tutorial. The first part is here: How to open a Text File in VB .NET
Deleting a file is quite simple - but dangerous! So be very careful when you're trying out this code. Make sure the file you're going to delete is not needed - you won't be able to restore it from the recycle bin!
To delete a file from your computer, you use the Delete method of System.IO. Here's some new code for you to try:
Dim FileToDelete As String
FileToDelete = "C:\Users\Owner\Documents\testDelete.txt"
If System.IO.File.Exists( FileToDelete ) = True Then
First, we've set up a string variable called FileToDelete. We've then assigned the name of a file to this variable - "C:\testDelete.txt". (We created this file first, and made sure that it was safe to junk it!)
Next, we test to see if the File Exists. In the IF Statement, we then had this:
System.IO.File.Delete( FileToDelete )
After selecting the Delete method, you type the name of the file you want to get rid of. This goes between a pair of round brackets.
And that's it! That's all you need to do to delete a file from your computer, never to see it again. So be very careful when you test out the code!
OK, enough of Text Files. In the next section, we'll look at Functions and Subs.
Buy the Book of this Course
Email us: enquiry at homeandlearn.co.uk