Skip to content

Instantly share code, notes, and snippets.

@JekRock
Created September 2, 2017 08:02
Show Gist options
  • Save JekRock/7ce4bb44483bb4a26492b45771e8979d to your computer and use it in GitHub Desktop.
Save JekRock/7ce4bb44483bb4a26492b45771e8979d to your computer and use it in GitHub Desktop.
Check is file locked
protected virtual bool IsFileLocked(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
{
if (stream != null)
stream.Close();
}
//file is not locked
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment