I have an application that IMPLICITLY opens a handle on a dll/file. At some point in the application, I want to release this handle. How can I do it? My application is in C#.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
What exactly you are trying to do? If you want to load an assembly to do some stuff with that, and then unload it completely, you need to rely on creating a new app domain.
Have a look at this post for more, http://blogs.msdn.com/suzcook/archive/2003/07/08/57211.aspx
Just use the Dispose or Close method of the class that opened the handle.
use PInvoke if you have an handler that you want to close
I don't know an easy way. Excessive implicit file locking is something I've always disliked about Windows.
If you need to replace the file, MoveFileEx can do it at next boot. You'd use it to rename or delete the original, and then rename something else into its place. http://msdn.microsoft.com/en-us/library/aa365240(VS.85).aspx http://www.pinvoke.net/default.aspx/kernel32/MoveFileEx.html
If you don't want to mess with the API directly there's MoveFile in the SysInternals suite which does the same: http://technet.microsoft.com/en-us/sysinternals/bb897556.aspx
Or you can have another program access the file when your program isn't running.
There's ways to get a list of handles per process, if you really want to try to close the handle, which would most likely just crash your program if .NET tries to access it again. It's not pretty, and the example is C++: http://www.codeguru.com/forum/showthread.php?t=176997