title: IO Exception loading Assembly can't find file
body: <p>I am loading a Assembly at run time in my application:</p>
  
  <pre><code>FileStream lDLLStream = lDLLPath.OpenRead();
  byte[] lDLLArray = new byte[lDLLStream.Length];
  lDLLStream.Read(lDLLArray, 0, (int)lDLLStream.Length);
  
  Assembly lAssembly = Assembly.Load(lDLLArray);
  </code></pre>
  
  <p>However, I am getting the following exception:</p>
  
  <pre><code>Could not load file or assembly 'UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
  </code></pre>
  
  <p>However, if I load a different DLL from the same directory that references both the DLL I was originally trying to load (and naturally UnityEngine as it has a dependency on that too), I get no error message.</p>
  
  <p>In fact, at load you can see the second (working) DLL has in its referenced assemblies precisely:</p>
  
  <pre><code>UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
  </code></pre>
  
  <p>So I am failing to see how if it can load the second DLL, what could be causing it to fail to load the first. </p>
  
  <p>Some additional information: The first (non-working) DLL is precisely the DLL generated by Unity (3D game engine) whereas the second DLL is a project I created in the Unity solution. </p>
  
  <p>My project is .NET 4.0 (and Unity is 3.5) but I tried setting my test DLL to compile to 3.5 and that didn't crash on load. </p>
  
  <p>I don't know if this would be better asked on Unity Answers?</p>
  
  <p>Update: OK maybe less odd that I thought. The second working DLL was only working since it wasn't using the DLL that it was failing to load so never got round to failing. Also, can get it to load the first DLL by dumping the Unity DLL in the executing directory, so I guess I just need to find a way of adding directories to where it should look. </p>
  
  <p>Update 2: So I have "fixed" this issue by listening to </p>
  
  <p><code>AppDomain.CurrentDomain.AssemblyResolve</code> </p>
  
  <p>And presenting a dialogue box to the user to find the missing DLL which has fixed the exception. I would appreciate knowing if there is a better way of doing this. Since the user is specifying custom DLLs so maybe this is the only reasonable thing to do. Otherwise, I will post this as an answer. </p>