Is Assembly.LoadFrom keeping an open file handle?

早过忘川 提交于 2019-12-06 05:12:56
Cédric Bignon

Yes, it is open until the assembly is unloaded from the appdomain.

If you really need to delete the file, load its content into memory. The use Assembly.Load(byte[]) to load the assembly:

using (Stream stream = File.OpenRead("path.exe"))
{
    byte[] rawAssembly = new byte[stream.Length];
    stream.Read(rawAssembly, 0, (int)stream.Length);
    Assembly.Load(rawAssembly);
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!