Created
September 2, 2017 08:02
-
-
Save JekRock/7ce4bb44483bb4a26492b45771e8979d to your computer and use it in GitHub Desktop.
Check is file 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
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