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

查看:266
本文介绍了在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.

如果您正在寻找一种执行更新的方法,那么我会选择一个存根exe,它会生成真正的AppDomain.然后,当该存根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天全站免登陆