Link to home
Start Free TrialLog in
Avatar of darkphotn
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:


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;
    }

Open in new window

Avatar of UnifiedIS
UnifiedIS

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
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you need to open in exclusive mode, like:
File.Open(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.None)