Skip to content

Instantly share code, notes, and snippets.

@nramsbottom
Created September 4, 2020 19:40
Show Gist options
  • Save nramsbottom/efc1ff7ef6ef7468ec2da749d2779a50 to your computer and use it in GitHub Desktop.
Save nramsbottom/efc1ff7ef6ef7468ec2da749d2779a50 to your computer and use it in GitHub Desktop.
C#: Check if file is locked
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