0

I want to load & unload assemblies that are found outside the current app domain. Upon reading the following threads,

  1. AppDomain.Load() fails with FileNotFoundException
  2. Unloading the Assembly loaded with Assembly.LoadFrom() <- I couldn't understand the answer from this thread
  3. Loading/Unloading assembly in different AppDomain <- I placed the target assembly folder location in the private bin path which resulted in FileNotFoundException

I have the following code that raises the FileNotFoundException.

// let's assume that the executable running this code is found at "F:\\Test.exe"

// this is the assembly I want to load
string pathToAssembly = "C:\\Documents\\hello.dll";

// copied directly from the 1st link
AppDomain dom = AppDomain.CreateDomain("some");     
AssemblyName assemblyName = new AssemblyName();
assemblyName.CodeBase = pathToAssembly;
Assembly assembly = dom.Load(assemblyName); // raises FileNotFoundException

I checked the documentation: https://learn.microsoft.com/en-us/dotnet/framework/app-domains/how-to-load-assemblies-into-an-application-domain https://learn.microsoft.com/en-us/dotnet/framework/app-domains/how-to-unload-an-application-domain

But it didn't use AppDomain to load AND unload from an absolute path.

Based from the 2nd link, AppDomains search for the assemblies within its current executable's directory path. Which, of course, I need to work around that because I need to load a managed library outside of the current directory.

And, for additional information, I'm on WPF using .NET framework 4.7.2

TLDR: What do I need to change the "look" directory of the AppDomain? or what can I do to fix this?

CraftedGaming
  • 499
  • 7
  • 21
  • The link you provided says the prefered way is to LOAD and give this link : https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.load?view=net-5.0 – jdweng Apr 28 '21 at 07:12
  • but that's just one part of my problem. I need to load **and unload** – CraftedGaming Apr 28 '21 at 07:16
  • Try : https://learn.microsoft.com/en-us/dotnet/api/system.appdomain.unload?view=net-5.0#System_AppDomain_Unload_System_AppDomain_ – jdweng Apr 28 '21 at 07:29
  • I would suggest you provide a full solution rather than pasting the same links I've provided. Like I said in the question, the microsoft documentation doesn't show completely how to use it. Do read the entirety of my question and problems before replying back – CraftedGaming Apr 28 '21 at 09:52
  • di you open my link and look at the example? The title of page may look similar but the example is different. – jdweng Apr 28 '21 at 11:52
  • your first link only showed how to load an assembly using `Assembly` which isn't what I want. your second link is similar to my link wherein it just created an `AppDomain` object and unloaded itself without loading any assembly. If you checked `AppDomain.Load()`, it needs either an `AssemblyName` object or the other parameters in the function overload. Then again, I *need* to get an assembly **outside** of the current executable's directory – CraftedGaming Apr 29 '21 at 01:57

0 Answers0