I want my application to clean all the temp files it used, the problem is that not all the temp files are under my control, so I just want to "brutally" unlock them in order to delete them programatically.
-
I don't know about your application and your files that you are using, but if you are using FileStream to manipulate with you files you must close the FileStream and Dispose the FileStream object and then your used file(s) will be unlocked. – milot Oct 28 '08 at 12:44
4 Answers
Take a look at this article. I think that you'll struggle to do this in C# natively, even using interop, but writing a C++/CLI wrapper assembly may be a good compromise. Note also that the user needs to have the SE_DEBUG privilege for this to work.

- 11,510
- 1
- 38
- 59
-
The ForceDel.zip file in that article (which is the solution to this answer) is no longer available... and I need it too! :( – kzfabi Jan 10 '13 at 12:56
-
Found it! Download the same [here](http://www.technipages.com/wp-content/uploads/2007/11/forcedel.zip). – kzfabi Jan 10 '13 at 13:07
I've struggled with this as well, and ended up just shelling out to Unlocker's command line implementation. In my case it has to run many times daily and ends up unlocking thousands of files per day without any problem.

- 1,323
- 1
- 14
- 22
Surely, of your application is wanting to clean up the temp files it owns, then you have full control to unlock these files and delete them!
If you're wanting to delete all TEMP files, whether owned by your application or otherwise, you should be VERY careful. The original application probably applied the lock because it wants to use the file!
If you truly need to, you could always spawn a command-line application rather than trying to replicate the functionality of existing tools which will be difficult in C#.

- 14,896
- 8
- 53
- 78
-
They files are part of my application but they're not part of my module and for political reasons I cannot modify that module (didn't really wanted to get into that details :-) ), anyway I know it is safe to brutally delete those files. – Pablo Retyk Oct 28 '08 at 11:55