问题
I am loading another assembly using
Assembly.LoadFrom("path.exe");
and after that i cant seem to delete that exe from the file system. so i was wondering if this path keeps an open file handle and how i can close it?
回答1:
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);
}
回答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.
来源:https://stackoverflow.com/questions/17838378/is-assembly-loadfrom-keeping-an-open-file-handle