How can I unlock a file that is locked by a process in .NET

Pablo Retyk picture Pablo Retyk · Oct 28, 2008 · Viewed 25.2k times · Source

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.

Answer

Stu Mackellar picture Stu Mackellar · Oct 28, 2008

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.