vb.net 删除单个 .txt 文件(如果存在于 Visual Basic 中)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30584744/
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
Delete a single .txt file if exist in Visual Basic?
提问by Maxime.L
i have been trying to figure out how to delete a .txt file that constantly change name exept the first 4 ex: THISTEXT-123-45.txt where THISTEXT stays the same but -123-45 changes.
我一直在试图弄清楚如何删除一个不断更改名称的 .txt 文件,除了前 4 个示例:THISTEXT-123-45.txt,其中 THISTEXT 保持不变,但 -123-45 更改。
I have found a way to detect it, but i don't know how to delete it.
我找到了检测它的方法,但我不知道如何删除它。
Dim paths() As String = IO.Directory.GetFiles("C:\", "THISTEXT*.txt")
If paths.Length > 0 Then
Anyone knows the command line to delete that special .txt file? I am using Visual Basic on visual studio 2013 framework 3.5.
任何人都知道删除那个特殊的 .txt 文件的命令行吗?我在 Visual Studio 2013 框架 3.5 上使用 Visual Basic。
采纳答案by Mark Hall
If you read the MSDN page on GetFiles, you will realize that you have the file name and path in your paths array. You can then iterate through the array deleting your matches.
如果您阅读 上的 MSDN 页面GetFiles,您将意识到您的路径数组中有文件名和路径。然后您可以遍历数组删除您的匹配项。
Dim x as Integer
Dim paths() as String = IO.Directory.GetFiles("C:\", "THISTEXT*.txt")
If paths.Length > 0 Then
For x = 0 to paths.Length -1
IO.File.Delete(paths(x))
Next
End If
回答by 0m3r
Use the Delete method of System.IO.
使用 System.IO 的 Delete 方法。
Assuming you have write access to C:\
假设您对 C:\ 具有写访问权限
Dim FileDelete As String
FileDelete = "C:\testDelete.txt"
If System.IO.File.Exists( FileDelete ) = True Then
System.IO.File.Delete( FileDelete )
MsgBox("File Deleted")
End If
Deleting a file is quite simple - but dangerous! So be very careful when you're trying out this code.
删除文件非常简单 - 但很危险!所以当你尝试这个代码时要非常小心。
EditTo delete all file use *(asterisk) followed with the file extension
编辑要删除所有文件使用 *(星号)后跟文件扩展名
example C:\*.txt"
例子 C:\*.txt"
For multiple files
对于多个文件
Dim FileDelete As String
FileDelete = "C:\"
For Each FileDelete As String In IO.Directory.GetFiles(FileDelete & "THISTEXT*.txt")
File.Delete(FileDelete)
Next
回答by AStopher
To build on the feedback you provided to Omar'sanswer, it appears that your file path and file name are separate.
根据您提供给Omar's answer的反馈,您的文件路径和文件名似乎是分开的。
You cannot provide them separated by a comma, as commas denote separate parameters passed to a subroutine or function.
您不能提供用逗号分隔的它们,因为逗号表示传递给子例程或函数的单独参数。
To fix this, you need to concatenate them, for example:
要解决此问题,您需要连接它们,例如:
Dim fileName As String = "foo.txt"
Dim filePath As String = "C:\"
Dim FileToDelete As String = fileName + filePath
To delete a single .*txtfile if it exists:
要删除单个.*txt文件(如果存在):
If (deleteFile("C:\")) Then
MsgBox("File deletion successful")
Else
MsgBox("File couldn't be deleted with the following error: " + exception)
End If
alternatively with concatenation:
或者串联:
If (deleteFile("C:\") Then
MsgBox("File deletion successful")
Else
MsgBox("File couldn't be deleted with the following error: " + exception)
End If
Dim exception As String 'Place this at the beginning of your app's class.
Dim path As String = "C:\"
If (deleteFile(path)) Then
MsgBox("File deletion successful")
Else
MsgBox("File couldn't be deleted with the following error: " + exception)
End If
Private Function deleteFile(ByVal dir) As Boolean
Dim fileToRemove As String
Try
Dim paths() As String = IO.Directory.GetFiles(dir, "THISTEXT*.txt")
For i As Integer = 0 To paths.Length
fileToRemove = paths(i).ToString
System.IO.File.Delete(fileToRemove)
If (Not System.IO.File.Exists(fileToRemove)) Then
Return True
Else
exception = "Unknown error."
Return False
End If
Next
Return False
Catch ex As Exception
exception = ex.Message
Return False
End Try
Return False
End Function
The above function checks if the file exists, if it does it tries to delete it. If the file cannot be deleted, or an error occurs (which is handled), the Function returns False.
上面的函数检查文件是否存在,如果存在则尝试删除它。如果无法删除文件,或发生错误(已处理),则函数返回False.
回答by Idle_Mind
Simple example:
简单的例子:
For Each path As String In IO.Directory.GetFiles("C:\", "THISTEXT*.txt")
Try
System.IO.File.Delete(path)
Catch ex As Exception
MessageBox.Show(path, "Unable to Delete File")
End Try
Next

