Question : Check if file in use and resume

Hi guys

Got a program that sits and checks for the existence of a file, if it is found it deletes it.

That works great but ive had the first complaint that an error was thrown when the prog ran on its timer, and the app that creates the file was still using it, giving a file in use error.

I have been trying to work out try/catch to get my prog to check if the file is in use, skip if it is or delete if it is not. but i cant work it out.

Any suggestions
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
Private Sub tmrA_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrA.Tick
            Dim FileToDelete As String
            Dim filetodelete1 As String


            FileToDelete = "C:\Documents and Settings\" + Environment.UserName + "\Local Settings\Temp\routerequest1.html"
            filetodelete1 = "C:\Users\" + Environment.UserName + "\appdata\local\temp\routerequest1.html"
            Try

                Dim FS As IO.FileStream = IO.File.Open(filetodelete1, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)

                FS.Close()
                FS.Dispose()
              
            If System.IO.File.Exists(FileToDelete) = True Then

                System.IO.File.Delete(FileToDelete)


            End If
            If System.IO.File.Exists(filetodelete1) = True Then

                System.IO.File.Delete(filetodelete1)

             

            End If

            Catch ex As IO.IOException



            
            End Try

Answer : Check if file in use and resume

Private Sub tmrA_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrA.Tick
      Dim FileToDelete As String
      Dim filetodelete1 As String
      Dim deleted As Boolean = False
      Dim fi1 As New System.IO.FileInfo(FileToDelete)
      Dim fi2 As New System.IO.FileInfo(filetodelete1)

      FileToDelete = "C:\Documents and Settings\" + Environment.UserName + "\Local Settings\Temp\routerequest1.html"
      filetodelete1 = "C:\Users\" + Environment.UserName + "\appdata\local\temp\routerequest1.html"

      While Not deleted
            Try
                  If fi1.Exists Then
                        fi1.Delete()
                  End If

                  If fi2.Exists Then
                        fi2.Delete()
                  End If

                  deleted = True
            Catch ex As IO.IOException
                  deleted = False
            End Try
      End While
End Sub
Random Solutions  
 
programming4us programming4us