HeyMyBoy
HeyMyBoy

Reputation: 3

Vb.net Delete File (CMD)

This is the code:

Private Sub DeleteFiles()
    Dim file As String
    Dim prefetchPath As String
    prefetchPath = Environment.GetEnvironmentVariable("windir", EnvironmentVariableTarget.Machine) & "\Prefetch"
    For Each file In IO.Directory.GetFiles(prefetchPath)
        If file.Contains("EXAMPLE.EXE") Then
            IO.File.Delete(file)
        End If
    Next
End Sub

If someone knows how to do the same thing with the cmd since the application doesn't delete the file if the application is open Thanks

Upvotes: 0

Views: 488

Answers (1)

Striezel
Striezel

Reputation: 3758

There are two potential problems with that:

  • On Windows you generally cannot delete files that are opened and still in use by an application. You need to close the application first.
  • The code is trying to delete a file inside the Windows directory, and that usually triggers UAC (user access control). That is, unless your program has administrative prvileges it may not have the permission to delete that file, even if it is not in use anymore. That can be solved by running the program as administrator, but be very careful which files you delete then. Deleting the wrong files in the Windows directory may cause your system to become instable (or worse: unusable).

Upvotes: 2

Related Questions