0

I have a scenario where I must delete a file. I don't know and don't care who holds the file. I must delete it and they can crush for all I care. (I don't want to kill the locking task)

the only solution that comes to my mind is to use http://www.emptyloop.com/unlocker/ command line interface.

MoveFileEx is not and option as I cannot restart the machine.

is there any more C#ish method/library for this?

im not thrilled using console application API

in case it is not clear.I know the risk involved and I don't need a lecture of why this is a bad practice. if you know how to do what I asked. thank you very much!

if you want to lecture why this is bad - just don't find someone else to bother##

Community
  • 1
  • 1
Nahum
  • 6,959
  • 12
  • 48
  • 69
  • 1
    So you're looking for the `BreakProgrammingGuaranteesForOtherProgramsPossiblyEvenOperatingSystemProcessesBecauseImAbsolutelyRight()`? I don't think that's been implemented in C#. – Damien_The_Unbeliever Aug 21 '14 at 06:20
  • Not really. If another program has an open handle on a file, and you delete it, wouldn't it affect the program that was using it? What if some program deletes your browser executable while you were typing in this question? – danish Aug 21 '14 at 06:22
  • 1
    I'm not sure if actually know the severity of this "bad practice". It's equivalent to a use-after-free bug, which are a form of memory corruption. If the handle has been reused when the target application ties to access it, it now accesses a different object which can lead to file corruptions and security holes. That said, the following blog post has a plausible solution: http://www.nakov.com/blog/2009/05/16/close-win32-handle-from-external-process/ – CodesInChaos Aug 28 '14 at 11:19
  • @ CodesInChaos post this as an answer so I can give you the credit. – Nahum Aug 31 '14 at 06:39

2 Answers2

1

I can't give you a solution but can point you into a direction.

Window's Process Explorer has a function that can make you search for handles: enter image description here

When you then select that handle you go to the process owning that handle and you can right click on it and Close the handle and also relieve the lock that process has on that file.

So basically you need to find out which API calls Process explorer is using and execute them yourself in your application.

Stormenet
  • 25,926
  • 9
  • 53
  • 65
0

I think what you are asking for is impossible due to the nature of a lock.
How would you feel if another program could just snatch your files and wipe them as you were reading data?

I believe these unlockers detect the process and try to force it to release it's lock 1 way or another(maybe even shutting their process down).
While this may work for most applications some will be more aggressive (think virus scanners for example).

So maybe you need to ask yourself if you want to increase your chances of getting a lock or if you need to be absolutely sure to get a lock.

Edit:
Assuming you can terminate the locker process and you really want to clear those files(no matter the consequences) you could find the process that holds the lock and shut it down. In this thread they give a few solutions for tracking which process holds a lock(in c# code) via either handle, win32 dll's or even plan .NET code.

Disclaimer
Be aware that shutting down a process like this will have a terrible impact on the consistency of that program and you might even do more bad then good(suppose it's writing it's status the the database for example and halfway it gets terminated)

Community
  • 1
  • 1
Kristof
  • 3,267
  • 1
  • 20
  • 30
  • my software runs alone on a server. any other program that may access the files. is also written by me. I need to handle the case of files I locked and where not released because of a leak. – Nahum Aug 21 '14 at 06:29
  • 1
    Including the operating system? – Kristof Aug 21 '14 at 06:30
  • of course not. but why the **** would it lock my files. – Nahum Aug 21 '14 at 06:32
  • 2
    @NahumLitvin - Have you considered fixing the bug instead? – Damien_The_Unbeliever Aug 21 '14 at 06:33
  • @Damien_The_Unbeliever the bug was fixed. but future similar problem will fill the servers hard drive in a few minutes. – Nahum Aug 21 '14 at 06:40
  • @NahumLitvin - All you are doing is putting band-aid/plasters over your own work, its better to spend the time fixing these issues, if similar problems do happen, then fix them too - rinse and repeat – Sayse Aug 21 '14 at 06:51
  • You are trying to handle the symptom which will introduce just more issues. If 2 processes need to write/access the same file and one process locks it exclusively on open, off course the other process wont be able to write to it. I think you need to address the core cause and re-look at your approach and current implementation to fix your problem. –  Aug 21 '14 at 06:55