Link to home
Start Free TrialLog in
Avatar of Member_2_1242703
Member_2_1242703

asked on

Handling files locked by other processes in C#

Is there a way to force a file that is locked by another process to be moved/copied in C#?
Avatar of Kimputer
Kimputer

That's an OS thing. Therefore, to identify the process you need to have extra code. Then you have to decide if it's safe to terminate the process to release the file (otherwise, there's no way to move it). Restrictions are obviously, that you need admin rights, and the code is meant for that machine (not a remote network file, since you need to query that server for the process of course)
You might be able to copy it - depends on just how the other process has opened it.  (It might be open for shared reading for instance).  You would have to attempt to open it as shared read.
Moving, as already said - No until the other process finishes and releases the file or you kill the other process off.
Avatar of Member_2_1242703

ASKER

Copy is fine. What are my options?
Since you probably already tried copying the normal way, it just means it's really locked exclusively.
Your options are still to kill the process in question, or to use some VSS type solution:
https://github.com/alphaleonis/AlphaVSS/tree/develop/src
Again, works on local machine and only with admin priv, not meant for remote systems.
Needs a lot of work, as VSS is not just a one line of code solution.
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland 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 can identify process and kill it, is this feasible?