如何通过C#释放句柄?

发布于 2024-07-30 18:02:38 字数 79 浏览 4 评论 0原文

我有一个应用程序隐式打开 dll/文件上的句柄。 在应用程序的某个时刻,我想释放这个句柄。 我该怎么做? 我的应用程序是用 C# 编写的。

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#.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

俏︾媚 2024-08-06 18:02:38

如果您有要关闭的处理程序,请使用 PInvoke

[System.Runtime.InteropServices.DllImport("Kernel32")]
private extern static Boolean CloseHandle(IntPtr handle);

use PInvoke if you have an handler that you want to close

[System.Runtime.InteropServices.DllImport("Kernel32")]
private extern static Boolean CloseHandle(IntPtr handle);
丢了幸福的猪 2024-08-06 18:02:38

你到底想做什么? 如果您想加载一个程序集来执行一些操作,然后将其完全卸载,则需要依赖于创建一个新的应用程序域。

public static void Main(string[] args)
{
AppDomain appDomain = AppDomain.CreateDomain("NewAppDomain");
appDomain.DoCallBack(new CrossAppDomainDelegate(AsmLoad));

// At this point, your assembly is locked, you can't delete

AppDomain.Unload(appDomain);
Console.WriteLine("AppDomain unloaded");

//You've completely unloaded your assembly. Now if you want, you can delete the same

}

public static void AsmLoad()
{
Assembly assembly = Assembly.LoadFrom(@"c:\Yourassembly.dll");

//Loaded to the new app domain. You can do some stuff here
Console.WriteLine("Assembly loaded in {0}",AppDomain.CurrentDomain.FriendlyName);
}

查看这篇文章了解更多信息,http://blogs .msdn.com/suzcook/archive/2003/07/08/57211.aspx

或者,如果您只是担心
保持文件锁定,您可以使用
影子复制。 这将制作一份副本
磁盘上的文件并将其加载
新地点。 原始文件
不会被该负载锁定。 到
这样做,设置
AppDomainSetup.ShadowCopyFiles 到
创建 AppDomain 时为“true”或
将 AppDomain.ShadowCopyFiles 设置为 true
创建完成后。

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.

public static void Main(string[] args)
{
AppDomain appDomain = AppDomain.CreateDomain("NewAppDomain");
appDomain.DoCallBack(new CrossAppDomainDelegate(AsmLoad));

// At this point, your assembly is locked, you can't delete

AppDomain.Unload(appDomain);
Console.WriteLine("AppDomain unloaded");

//You've completely unloaded your assembly. Now if you want, you can delete the same

}

public static void AsmLoad()
{
Assembly assembly = Assembly.LoadFrom(@"c:\Yourassembly.dll");

//Loaded to the new app domain. You can do some stuff here
Console.WriteLine("Assembly loaded in {0}",AppDomain.CurrentDomain.FriendlyName);
}

Have a look at this post for more, http://blogs.msdn.com/suzcook/archive/2003/07/08/57211.aspx

Or, if you're only worried about
keeping the file locked, you could use
shadow copying. That will make a copy
of the file on disk and load it from
the new location. The original file
will not be locked by that load. To
do that, set
AppDomainSetup.ShadowCopyFiles to
"true" when creating the AppDomain or
set AppDomain.ShadowCopyFiles to true
after it's already been created.

第七度阳光i 2024-08-06 18:02:38

只需使用打开句柄的类的 Dispose 或 Close 方法即可。

Just use the Dispose or Close method of the class that opened the handle.

匿名的好友 2024-08-06 18:02:38

我不知道一个简单的方法。 Windows 中过多的隐式文件锁定是我一直不喜欢的。

如果您需要替换文件,MoveFileEx 可以在下次启动时完成。 您可以使用它来重命名或删除原始内容,然后将其他内容重命名到其位置。
http://msdn.microsoft.com/en-us /library/aa365240(VS.85).aspx
http://www.pinvoke.net/default.aspx/kernel32/MoveFileEx。 http://technet.microsoft.com/en-us/sysinternals/bb897556

如果您不想直接使用 API,SysInternals 套件中的 MoveFile 也可以实现相同的功能:http://technet.microsoft.com/en-us/sysinternals/bb897556.aspx

或者,您可以在您的程序未运行时让其他程序访问该文件。

如果您确实想尝试关闭句柄,可以通过多种方法获取每个进程的句柄列表,如果 .NET 尝试再次访问它,这很可能会使您的程序崩溃。 这不太漂亮,示例是 C++:
http://www.codeguru.com/forum/showthread.php?t= 176997

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文