在c#中执行然后删除一个DLL [英] Executing and then Deleting a DLL in c#

查看:33
本文介绍了在c#中执行然后删除一个DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个自我更新的应用程序,其中大部分代码都在一个单独的 DLL 中.它是命令行,最终将在 Mono 上运行.我只是想让这段代码在命令行的 Windows 上的 C# 中工作.

I'm creating a self updating app where I have the majority of the code in a seperate DLL. It's command line and will eventually be run on Mono. I'm just trying to get this code to work in C# on windows at the command line.

如何创建一个可以在运行时删除支持的 dll 的 c# 应用程序?

How can I create a c# application that I can delete a supporting dll while its running?

AppDomain domain = AppDomain.CreateDomain("MyDomain");
ObjectHandle instance = domain.CreateInstance( "VersionUpdater.Core", "VersionUpdater.Core.VersionInfo");
object unwrap = instance.Unwrap();
Console.WriteLine(((ICommand)unwrap).Run());
AppDomain.Unload(domain);
Console.ReadLine();

在 ReadLine 处,VersionUpdater.Core.dll 仍被锁定无法删除

at the ReadLine the VersionUpdater.Core.dll is still locked from deletion

ICommand 接口位于 VersionUpdater.Common.dll 中,Commandline 应用程序和 VersionUpdater.Core.dll 都引用它

The ICommand interface is in VersionUpdater.Common.dll which is referenced by both the Commandline app and VersionUpdater.Core.dll

推荐答案

我曾经管理过类似问题的唯一方法是将 DLL 放在与试图删除它的程序集不同的 AppDomain 中.我卸载了另一个 AppDomain,然后从磁盘中删除了 DLL.

The only way I've ever managed something similar is to have the DLL in a separate AppDomain to the assembly that is trying to delete it. I unload the other AppDomain and then delete the DLL from disk.

如果您正在寻找一种执行更新的方法,我会选择一个生成真正 AppDomain 的存根 exe.然后,当该存根 exe 检测到要应用更新时,它会退出另一个 AppDomain,然后执行更新魔术.

If you're looking for a way to perform the update, off the top of my head I would go for a stub exe that spawns the real AppDomain. Then, when that stub exe detects an update is to be applied, it quits the other AppDomain and then does the update magic.

更新程序不能与它正在更新的东西共享 DLL,否则它会锁定这些 DLL,从而防止它们被删除.我怀疑这就是为什么您仍然会遇到异常的原因.更新程序必须是独立的,不依赖于其他 AppDomain 使用的任何内容,反之亦然.

The updater cannot share DLLs with the thing it is updating, otherwise it will lock those DLLs and therefore prevent them from being deleted. I suspect this is why you still get an exception. The updater has to be standalone and not reliant on anything that the other AppDomain uses, and vice versa.

这篇关于在c#中执行然后删除一个DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆