darkphotn
asked on
How to determine if file is locked?
How do I determine if a file is locked?
If a file is readonly but not locked, the following fails:
If a file is readonly but not locked, the following fails:
public bool isLocked(string s) {
if (!File.Exists(s)) return false;
try { FileStream f = File.Open(s, FileMode.Open, FileAccess.ReadWrite); f.Close(); }
catch{return true; }
return false;
}
In VB.NET, I add a catch for io.ioexception in addition to a generic catch ex as exception. Not sure if you can do that in C# but it would seem like you could.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you need to open in exclusive mode, like:
File.Open(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
File.Open(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.None)