-1

I have WPF application AppTest.

DllMaster1 - This is only a window with controls.

In the main application refers to a class library DllMaster1.

Window app = new DllMaster1.MainWindow();
app.ShowDialog();
app = null;
GC.Collect();

Now I need to remove an object from memory, so that I can replace him with another, because when I want to replace the DllMaster1 I get the message:

The action can't be completed because the folder or a file in it is open in another program.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 25 '15 at 18:16

2 Answers2

0

Once a Dll is loaded in to an AppDomain it cannot be unloaded without unloading the entire AppDomain. You can't release the main AppDomain because it is the one that owns the executing application. The solution is to create a new AppDomain and load the dll inside that one. When you are done with it you can unload the 2nd AppDomain, which will release the lock on the dll.

You can find code Here: Using AppDomain in C# to dynamically load and unload dll

Keep in mind that this is NOT a simple thing to do; there is a lot of complexity and subtle behavior can cause some really interesting bugs.

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
0
        AppDomain appDomain = AppDomain.CreateDomain("appDomain");
        Assembly a = appDomain.Load("DllMaster1");
        Type myType = a.GetType("DllMaster1.MainWindow");
        MethodInfo myMethod = myType.GetMethod("MyMethod");
        object obj = Activator.CreateInstance(myType);
        myMethod.Invoke(obj, null);
        AppDomain.Unload(appDomain);

MyMethod is new method in DllMaster1 this.Show();. Unload does not work, I still can not remove dll.

Vimal CK
  • 3,543
  • 1
  • 26
  • 47