clamp
clamp

Reputation: 34036

Is Assembly.LoadFrom keeping an open file handle?

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?

Upvotes: 3

Views: 1476

Answers (2)

Cédric Bignon
Cédric Bignon

Reputation: 13022

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);
}

Upvotes: 7

Ian Griffiths
Ian Griffiths

Reputation: 14557

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.

Upvotes: 2

Related Questions