1 public static bool IsFileLocked(string file) 2 { 3 try 4 { 5 using (File.Open(file, FileMode.Open, FileAccess.Write, FileShare.None)) 6 { 7 return false; 8 } 9 } 10 catch (IOException exception) 11 { 12 var errorCode = Marshal.GetHRForException(exception) & 65535; 13 return errorCode == 32 || errorCode == 33; 14 } 15 catch (Exception) 16 { 17 return false; 18 } 19 }