vb.net 如何使用VB.NET删除文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21220384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:23:51  来源:igfitidea点击:

How to delete a file using VB.NET?

vb.net

提问by ShaneRibz

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:

我的程序中有一个按钮,单击时会删除某个文件,例如example.txt。我必须删除它的代码是:

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?

但是文件还在。我做了一些研究,大多数人都说它应该有效。为什么这不起作用?或者这段代码是错误的?

回答by Daniel Charry

This one doesn't require so much mystery :)

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

My.Computer.FileSystem.DeleteFile(ADDRESS_OF_FILE_AS_STRING)

回答by ChoiceMC

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:

要从计算机中删除文件,请使用 System.IO 的 Delete 方法。这里有一些新代码供您尝试:

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

回答by Hahayahahay

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