如果存在于Visual Basic中,则删除单个.txt文件?

时间:2015-06-01 23:04:58

标签: vb.net visual-studio-2013

我一直试图弄清楚如何删除一个不断更改名称的.txt文件,除了前面的4 ex:THISTEXT-123-45.txt,其中THISTEXT保持不变但-123-45更改。

我找到了检测它的方法,但我不知道如何删除它。

SwingUtilities.invokeLater(new Runnable() {
    public void run() {

        new ActionProgram().setVisible(true);

    }
});

任何人都知道删除该特殊.txt文件的命令行? 我在visual studio 2013 framework 3.5上使用Visual Basic。

4 个答案:

答案 0 :(得分:2)

使用System.IO的Delete方法。

假设您具有对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
  

删除文件非常简单 - 但很危险!所以在你试用这段代码时要非常小心。

修改 要删除所有文件,请使用*(星号),然后删除文件扩展名

示例C:\*.txt"

对于多个文件

Dim FileDelete As String

FileDelete = "C:\"

For Each FileDelete  As String In IO.Directory.GetFiles(FileDelete & "THISTEXT*.txt")
    File.Delete(FileDelete)
Next

答案 1 :(得分:2)

如果您阅读GetFiles上的MSDN页面,您会发现路径数组中有文件名和路径。然后,您可以遍历数组删除匹配项。

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

答案 2 :(得分:1)

根据您提供给Omar's answer的反馈,您的文件路径和文件名似乎是分开的。

您不能用逗号分隔它们,因为逗号表示传递给子例程或函数的单独参数。

要解决此问题,您需要将它们连接起来,例如:

Dim fileName As String = "foo.txt"
Dim filePath As String = "C:\"

Dim FileToDelete As String = fileName + filePath

删除单个.*txt文件(如果存在):

If (deleteFile("C:\")) Then 
    MsgBox("File deletion successful")
Else
        MsgBox("File couldn't be deleted with the following error: " + exception)
End If

或者连接:

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

上述函数检查文件是否存在,如果存在则尝试删除它。如果无法删除文件,或发生错误(已处理),则函数返回False

答案 3 :(得分:0)

简单示例:

    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