简体   繁体   中英

How to delete a file using VB.NET?

I have a button in my program that will delete a certain file when clicked, such as example.txt. The code I have to delete it is:

File.Delete("example.txt")

But the file is still there. I have done some research and most people say that it should work. Why is this not working? Or is this code wrong?

这个不需要那么多神秘:)

My.Computer.FileSystem.DeleteFile(ADDRESS_OF_FILE_AS_STRING)

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

System.IO.File.Delete( FileToDelete )
MsgBox("File Deleted")

End If
 System.IO.File.Delete( "filepath" ) 

try this it works for me

        My.Computer.FileSystem.DeleteFile("the file",
       FileIO.UIOption.AllDialogs,
       FileIO.RecycleOption.SendToRecycleBin,
       FileIO.UICancelOption.DoNothing)

put this where you want to execute it. This sends to recycle bin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM