Created
September 4, 2020 19:40
-
-
Save nramsbottom/efc1ff7ef6ef7468ec2da749d2779a50 to your computer and use it in GitHub Desktop.
C#: Check if file is locked
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static bool IsFileLocked(string path) | |
{ | |
try | |
{ | |
using var fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None); | |
} | |
catch (IOException ex) | |
{ | |
if (ex.HResult == -2147024864) | |
return true; // file is locked | |
else | |
return true; // otherwise inaccessible | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment