You could try the code provided in this question over here, or look at other suggestions here.
The general approach is to enumerate the handles of all processes, get the file paths of those handles, and compare against the file you're interested in.
But a problem with this approach is that even if you can determine that the file is locked and which application has the file lock then you will still have to cope with race conditions, for example...
one millisecond later
- the file is not locked
- the application that did hold the lock is now not
then two milliseconds later
- the file is locked (again)
- a different application has the lock
then three milliseconds later
- the file is still locked
- yet another app has the lock
...etc
One suggestion is to attempt to get the file handle in your app, and catch the exception when you can't.
try
{
using (Stream stream = new FileStream("MyFilename.txt"))
{
}
} catch {
//check here why it failed and ask user to retry if the file is in use.
}
Of course this won't help identify the culprit(s) but at least you have a safer way of attempting to access the file.