简体   繁体   English

如何从C#中的子文件夹导入.dll库

[英]How do I import .dll library from subfolder in C#

When I have the library in the same folder as app I can simply: 当我将库放在与app相同的文件夹中时,我可以简单地:

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

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

I also have this in my app.config 我的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'm able to load this library. 我可以加载这个库。 Problem is that it is using some config files which needs to be in app run directory. 问题是它正在使用一些需要在app运行目录中的配置文件。 Is there some possibility how to tell the library that files it needs are in the same folder as the library? 是否有可能告诉库它需要的文件是否与库中的文件夹相同?

Since the DLL searches for the config in the current directory, it makes sense to temporarily change the current directory before loading it: 由于DLL在当前目录中搜索配置,因此在加载之前临时更改当前目录是有意义的:

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

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

Directory.SetCurrentDirectory(saveCurDir);

You can do this by adding a probing tag to your app's .config file. 您可以通过向应用程序的.config文件添加探测标记来完成此操作。 For example, if you wanted libraries to be loaded from a /lib/ subdirectory, your config would look like this: 例如,如果您希望从/ lib /子目录加载库,则您的配置将如下所示:

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM