How to import DLL from subfolder in C #

When I have a library in the same folder as the application, I can simply:

[DllImport("kernel32")]
public extern static IntPtr LoadLibrary(string librayName);

IntPtr iq_dll = LoadLibrary("IQPokyd.dll");

      

I also have this in my app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="plugins;" />
    </assemblyBinding>
  </runtime>
</configuration>

      

I can download this library. The problem is that it uses some configuration files that should be in the application launcher directory. Is there some way to tell the library that the files it needs are in the same folder as the library?

+3


source to share


2 answers


Since the DLL looks for configuration in the current directory, it makes sense to temporarily change the current directory before loading it:



string saveCurDir = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(Path.Combine(Application.StartupPath, "plugins"));

IntPtr iq_dll = LoadLibrary("IQPokyd.dll");

Directory.SetCurrentDirectory(saveCurDir);

      

+1


source


You can do this by adding a test tag to your application's .config file. For example, if you wanted libraries to be loaded from the / lib / subdirectory, your configuration would look like this:



<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="lib"/>
    </assemblyBinding>
  </runtime>
</configuration>

      

0


source







All Articles