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:\testDelete.txt"
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
MsgBox("File Deleted")
End If
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!
0 comments:
Post a Comment