Even though you are loading the assembly in the context of the new appdomain
you are returning a reference to it in the default appdomain, which causes
it to load the assembly in both appdomains. You need to write a class that
is remoted back to the default appdomain, and provide a method, e.g.
LoadAssembly() that does the actual loading.
This link explains many of the concepts and has some sample code that ought
to get you started.
http://www.gotdotnet.com/team/clr/Ap...#_Toc514058498
"Lauren Hines" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
> I have read numerous post stating that the only way to unload an assembly
> (DLL in my case) is to create a separate AppDomain, load the assembly,
then
> unload it by calling AppDomain.Unload.
>
> When trying to delete the DLL file I get an exception that access is
denied.
> When trying to copy over the DLL file, I get an exception that it is being
> used by another process.
>
>
> Can anyone tell me what is wrong with the following code?
> Much obliged.
> Lauren
>
> // must create a domain for unloading
>
> System.Security.Policy.Evidence baseEvidence =
> AppDomain.CurrentDomain.Evidence;
>
> System.Security.Policy.Evidence adevidence = new
> System.Security.Policy.Evidence(baseEvidence);
>
> AppDomain ad = AppDomain.CreateDomain("Test",adevidence);
>
>
> Assembly SampleAssembly;
>
>
>
> // FileInfoManager is the name of my dll
>
> // I have tried both of the following lines
>
> //SampleAssembly = Assembly.Load("FileInfoManager,Version=1.0.0.0");
>
> SampleAssembly = ad.Load("FileInfoManager,Version=1.0.0.0");
>
>
>
>
> //unload the dll
>
> AppDomain.Unload(ad);
>
> ad = null;
>
> SampleAssembly = null;
>
> string oldDLL = System.Windows.Forms.Application.StartupPath +
> "\\FileInfoManager.dll";
>
> string newDLL = System.Windows.Forms.Application.StartupPath +
> "\\FileInfoManager2.dll";
>
> try
>
> {
>
> File.Delete(oldDLL);
>
> }
>
> catch(Exception exc)
>
> {
>
> log.WriteEntry("Exception deleting file " + oldDLL + " : " +
> exc.Message,System.Diagnostics.EventLogEntryType.Error);
>
> }
>
>
> File.Copy(newDLL,oldDLL,true);
>
> File.Delete(newDLL);
>
>