加载后删除程序集 [英] Delete an assembly after loading
问题描述
我正在尝试使用以下代码加载 MSIL 程序集:
I am trying to load a MSIL assembly using the following code :
string PathOfDll = "PathOfMsILFile (Dll)";
Assembly SampleAssembly;
SampleAssembly = Assembly.LoadFrom(PathOfDll);
在这个程序结束时我应该删除这个文件:
At the end of this program I should delete this file :
File.Delete(PathOfDll);
它导致错误:'System.UnauthorizedAccessException'
It causes an error : 'System.UnauthorizedAccessException'
Additional information: Access to the path 'Path' is denied .
这与 UAC 无关,只是因为我在程序开始时加载程序集,当我想手动删除它时,它说该文件正在 vshost.exe 中使用.所以我说这只是为了表明它是为了加载程序集!
It is not relating to UAC it is just because I am loading the assembly at the start of program and when I wanna delete it manually it says that the file is in use in vshost.exe . So I say this just to show that it is for loading assemly !
那么有没有办法摆脱它(比如卸载这个程序集)?
So is there any way to get rid of it (something like Un-loading this assembly) ?
注意:我正在编写代码来运行垃圾收集器,但这个问题仍未解决.
Note : I am writing a code to run Garbage Collector but this problem is still unsolved .
谢谢.
推荐答案
一种可能的方法是:代替 LoadFrom
,使用 Load
,如下所示.
One possible way could be: Instead of LoadFrom
, use Load
as shown below.
Assembly asm = null;
try
{
asm = Assembly.Load(File.ReadAllBytes(path));
}
catch(Exception ex)
{
}
这篇关于加载后删除程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!