Howdy folks,
I wrote a small windows application that allows the user to drag and
drop files from Windows Explorer. The program attempts to load
each file as an assembly in a try-catch block. If it's a valid
assembly, it displays information about it in a data grid.
The problem is, the application has some sort of lock on the file from
that point until the application is closed. After I get the
information I want from the assembly (version, etc), I set the assembly
variable in the code to null. Still, when I try to delete a file
in Windows Explorer, it says that it's being used by another
program. Does anybody know how I can tell my application to
release whatever lock it has on the file
Thanks,
Dan

How to avoid locking a file after reading it?
Dennis Pilarinos
Imran, you're a genius! Your code works perfectly and no longer locks the files. The only change was that the FileStream declaration needed to be System.IO.FileStream(@"C:\MyAssembly.dll", System.IO.FileMode.Open, System.IO.FileAccess.Read) because it was throwing an exception when trying to read read-only files.
Thanks so much for all your help, it works great now!
Dan
Lior Canetti
As Derek mentioned, once an assembly is loaded into an AppDomain, it cannot be unloaded until the AppDomain itself has been unloaded. In most cases, this translates into "until your application closes". Also, using Assembly.Load and specifying the filename for the assembly does lock the file which means you can't do anything to the file until your application closes down. One way is to load the assembly in another AppDomain. Another way is to use the overload of Assembly.Load that takes in an array of bytes. Here's how you can do that:
[VB.NET]
[C#]
hope that helps,
Imran.
ChrisSabien
When you load an assembly dynamically you load it into an AppDomain, assemblies cannot be unloaded from an AppDomain unless the AppDomain is completely destroyed.
This is fairly detailed stuff, gets right into the foundations of the .NET framework, and I have no idea how it all works in code. If you do a search for AppDomain or even better search for creating add-ins and you'll find the right information you need.
http://www.gotdotnet.com/team/clr/AppdomainFAQ.aspx
What your doing by loading an Assembly is partially what you do to load custom add-ins for your application.
There's an easier way to solve this problem though, copy the assembly to a temporary directory, load that copy, and clear the directory when your application closes. Leave the original alone and it won't be locked.