2

I'm trying to delete a bunch of files, but some of them are locked (in use). I simply want to program to just skip over the locked ones and keep going, but the OS (Windows7) pops up a message each time it tries to delete one of these files and tells me it is in use. I just want to suppress the error and leave the file there and keep trying to delete the others.

My only options for "FileIO.UIOption" is "All" or "Errors Only", I want "None".

For Each foundFile As String In My.Computer.FileSystem.GetFiles(Application.StartupPath, FileIO.SearchOption.SearchTopLevelOnly, "*.old")
    Try
       My.Computer.FileSystem.DeleteFile(foundFile, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
    Catch ex As Exception
    End Try
 Next

Solved - See below

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
John
  • 1,310
  • 3
  • 32
  • 58

3 Answers3

3

First step would be actually using File.Delete, haha.

IO.File.Delete won't show the dialogs, it will just come back as an IOException, so simply altering what you already have will work.

    For Each foundFile As String In My.Computer.FileSystem.GetFiles(Application.StartupPath, FileIO.SearchOption.SearchTopLevelOnly, "*.old")
        Try
            IO.File.Delete(foundFile)
        Catch ex As IO.IOException
            ' File is in use.
        End Try
    Next
Justin Packwood
  • 337
  • 2
  • 12
  • Thanks, but I tried this method and I get a VB runtime error stating that "access is denied" when trying to delete the file. So it is not even catching the exception. – John May 29 '14 at 16:02
  • @user2721815 - you may have set your debugging to break when an exception is thrown. Try switching that off (Debug>Exceptions menu) – Matt Wilko May 29 '14 at 16:06
  • Thanks Matt, but I don't know which one to change. The error shows up after I compile it when running it outside of VS. – John May 29 '14 at 16:11
  • @user2721815 - With the code I have we are only catching IOExceptions, try changing the 'IOException' to 'Exception' and that should work. It's not quite as specific but it should help. Also, if you don't know how to navigate the Exceptions settings as Matt pointed out, try hitting the "Reset All" button. – Justin Packwood May 29 '14 at 17:25
1

This is what I ended up using:

Private Sub delete_file()

Dim file_open As Boolean = False
Dim stream As FileStream = Nothing

For Each foundFile As String In My.Computer.FileSystem.GetFiles(Application.StartupPath, FileIO.SearchOption.SearchTopLevelOnly, "*.old")

    file_open = False

    Try
      stream = File.Open(foundFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
    Catch ex As Exception
      If TypeOf ex Is IOException AndAlso IsFileLocked(ex) Then
         file_open = True
      End If
    Finally
      If Not IsNothing(stream) Then
        stream.Close()
      End If
    End Try

    If file_open = False Then
      Try
        My.Computer.FileSystem.DeleteFile(foundFile, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
      Catch ex As Exception
        End Try
    End If

Next

End Sub

Private Shared Function IsFileLocked(exception As Exception) As Boolean
    Dim errorCode As Integer = Marshal.GetHRForException(exception) And ((1 << 16) - 1)
    Return errorCode = 32 OrElse errorCode = 33
End Function

Which I derived from the contributions to this question and the following question: VB.NET Checking if a File is Open before proceeding with a Read/Write?

Community
  • 1
  • 1
John
  • 1,310
  • 3
  • 32
  • 58
0

This is a c# work around worked for me you can convert it into VB and use it bro

protected virtual bool IsFileInUse(FileInfo file) { FileStream FS = null;

try
{
    FS = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
    //File Locked or in use
    return true;
}
finally
{
    if (FS != null)
        FS.Close();
}
//return if file is in unlicked state
return false;

}

  • I converted this to VB and when I run it, I get the same error message when simply trying to "Open" the file. So if the file is in use, I can't "open" it either. – John May 29 '14 at 16:03
  • Actually, this worked, but it had an error I didn't notice. In the file.open statement, you are missing the first argument (the file name). I don't kow how to give you credit and post the corrected VB result. – John May 29 '14 at 16:25