public static Boolean IsFileLocked(FileInfo file) { try { using (file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)) { return(false); } } catch //(IOException) { // Could not open file -> probably we have no // write access to the file return(true); } }
// https://stackoverflow.com/a/937558 public static bool IsFileLocked(this FileInfo file) { FileStream stream = null; try { stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None); } catch (IOException) { //the file is unavailable because it is: //still being written to //or being processed by another thread //or does not exist (has already been processed) return(true); } finally { stream?.Close(); } //file is not locked return(false); }
public override Stream Open(FileMode mode) { return(instance.Open(mode)); }