Search code examples
c#.netreferencefilenotfoundexceptioncopy-local

Copy local = false file not found exception issue


Hi I know this has been asked but it did not get an answer. I have a problem when I want to use a dll that is installed on C:\Program files (x86)\Dummu_API.dll

When I run my app it throws exception:

Could not load file or assembly 'Dummy_API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

My project have the reference as Copy Local = false and specific version = false. My project also is a x86 platform target.

I don´t know why my app don't find the assembly. Any Idea what it is happening?


Solution

  • Option 1

    You can tell your application that you will resolve the Assemblies yourselves if not found in references. To do that:

    In your applications main method attach assembly resolver:

    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
    

    Make sure you are not using the dynamically resolved assembly in your main method.

    Your resolver method code (This is where you load an assembly - look at ResolveEventArgs I have just hard coded one but you can resolve various assemblies from different locations here)

    static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        return Assembly.LoadFile(@"C:\temp\ClassLibrary1.dll");
    }
    

    Option 2

    Add this to your app.config (Only works for application base directory's sub folders)

    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <probing privatePath="PathToMyAssemblies\" />
      </assemblyBinding>
    </runtime>