problem with releasing loaded dll

G

Guest

Hi,

I try to load an assembly, use it and then release it. I am wondering if
there is a way to release the assembly?
I've read that if you use the current AppDomain this is not possible, so you
have to create a second domain to load the assembly, use it and unload the
domain. I use the following code:

//I have an dll called test in the current directory that I want to load
//The assembly is not domain-neutral
string lAssemblyName = Directory.GetCurrentDirectory() + @"\Test.dll";

//Create application domain setup and set the base directory to the current
directory
AppDomainSetup AppDomSetup = new AppDomainSetup();
AppDomSetup.ApplicationBase = Directory.GetCurrentDirectory();

//Create the new domain and load the assembly
AppDomain NewDomain = AppDomain.CreateDomain("SecDomain", null, AppDomSetup);
NewDomain.Load("Test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");

//After using it (not shown here), unload the new domain
AppDomain.Unload(NewDomain);

//Try to copy a dll over the unloaded dll (with overwrite = true);
File.Copy(@"C:\Test1.dll", lAssemblyName, true);


This gives the following exception:
The process cannot access the file '..\Test.dll' because it is being used by
another process.

So the dll is still not released. Is there a way to release it instead of
shutting down the whole process?

kind regards,
Koert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top