Is there a way to check if a file is in use?
Problem:
If a file is in use it will most likely be in a state of “READ ONLY”. This state or status may break programs or processes that need to access the file and write to it.
Solution:
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;
}
Resources:
https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use