code_junkie

How do I determine if an IOException is thrown because of a sharing violation?

Nov 13th, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. private void RobustMoveFile( System.IO.DirectoryInfo destinationDirectory, System.IO.FileInfo sourceFile, Boolean retryMove )
  2. {
  3. try
  4. {
  5. string DestinationFile = Path.Combine( destinationDirectory.FullName, sourceFile.Name );
  6. if ( File.Exists( DestinationFile ) )
  7. sourceFile.Replace( DestinationFile, DestinationFile + "Back", true );
  8. else
  9. {
  10. sourceFile.CopyTo( DestinationFile, true );
  11. sourceFile.Delete();
  12. }
  13. }
  14. catch ( System.IO.IOException IOEx )
  15. {
  16. int HResult = System.Runtime.InteropServices.Marshal.GetHRForException( IOEx );
  17. const int SharingViolation = 32;
  18. if ( ( HResult & 0xFFFF ) == SharingViolation && retryMove )
  19. RobustMoveFile( destinationDirectory, sourceFile, false );
  20. throw;
  21. }
  22. }
Add Comment
Please, Sign In to add comment