Code:
Dim Success As Boolean
' While Success flag is false...
While Success = False
' Try..Catch block that catches possible IOExceptions including 'The process cannot access the file...'
Try
' Hope for the best...
Success = True
' ...and try to move the file
File.Move(sourcePath, destPath)
' Catch IO type of exception
Catch ex As IOException
If ex.Message.StartsWith("The process cannot access the file") = False Then
' TODO: Write log and trace entries
Else
' If this is 'expected kind of exception' set flag to false to continue the loop
Success = False
End If
' Catch some general exceptions
Catch ex As Exception
' TODO: Write log and trace entries
End Try
End While
Dim Success As Boolean
' While Success flag is false...
While Success = False
' Try..Catch block that catches possible IOExceptions including 'The process cannot access the file...'
Try
' Hope for the best...
Success = True
' ...and try to move the file
File.Move(sourcePath, destPath)
' Catch IO type of exception
Catch ex As IOException
If ex.Message.StartsWith("The process cannot access the file") = False Then
' TODO: Write log and trace entries
Else
' If this is 'expected kind of exception' set flag to false to continue the loop
Success = False
End If
' Catch some general exceptions
Catch ex As Exception
' TODO: Write log and trace entries
End Try
End While
Ovo radi, ali mi se jednostavno ne sviđa. Grešku hvata preko Message property-ja, što je bezveze jer je taj property podložan lokalizaciji .Net Framework-a.
Znači, interesuje me:
1. Da li mogu na neku foru da saznam kada je file slobodan, tj. da nijedan drugi proces ne drži lock na njemu, ili jednostavno kada je proces kreiranja gotov?
2. Da li mogu da na neki bezbedniji način uhvatim 'pravi' exception. Jedino što mi ovde deluje kao mogućnost je da nasledim IOException klasu, pa da preko HResult property-ja uhvatim kod te greške. Ali to još nisam probao, tako da ne znam da li je moguće.
Ima li neko ideju?
Oliver 'FAQ' Klaćik