3

我正在使用加载另一个程序集

Assembly.LoadFrom("path.exe");

之后我似乎无法从文件系统中删除该 exe。所以我想知道这个路径是否保持打开的文件句柄以及如何关闭它?

4

2 回答 2

7

是的,在从 appdomain 卸载程序集之前,它一直处于打开状态。

如果您确实需要删除文件,请将其内容加载到内存中。Assembly.Load(byte[])加载程序集的用途:

using (Stream stream = File.OpenRead("path.exe"))
{
    byte[] rawAssembly = new byte[stream.Length];
    stream.Read(rawAssembly, 0, (int)stream.Length);
    Assembly.Load(rawAssembly);
}
于 2013-07-24T15:25:38.913 回答
2

By default, files will be locked, but .NET has a feature called Shadow Copies, in which it will make a copy of the assembly and load that instead. ASP.NET relies on this to enable web sites to be updated without running into these locking issues.

See this Shadow Copying Assemblies topic on MSDN for details.

于 2013-07-24T15:29:11.320 回答