简体   繁体   English

从c#中SetdllDirectory中指定的路径加载dll

[英]Loading dlls from path specified in SetdllDirectory in c#

I am new in dotnet.I have a dotnet dll that loads a c dll by using DllImport . 我是dotnet中的新手。我有一个使用DllImport加载c dll的dotnet dll。 I want to place all the dlls in a folder which is diffrent from the location of application. 我想将所有dll放在一个与应用程序位置不同的文件夹中。 I dont want to modify environmental variables. 我不想修改环境变量。 So i used setdlldirectory API and load my c# assembly through Assembly.Loadfrom(..) . 所以我使用了setdlldirectory API并通过Assembly.Loadfrom(..)加载我的c#程序集。 I checked that SetdllDirectory is working fine by verifying the value of GetDllDirectory(..) . 我通过验证GetDllDirectory(..)的值来检查SetdllDirectory是否正常工作。 But it is neither loading C# dll nor c dll from that folder. 但它既不从该文件夹加载C#dll也不加载c dll。 I am able to load C# dll by specyfing the path in Assembly.Loadfrom . 我可以通过在Assembly.Loadfrom指定路径来加载C# dll。 But not able to load c dll. 但是无法加载c dll。

Thanks in advance!! 提前致谢!!

I'd suggest adding the directory path to PATH env variable in runtime, using the following code: 我建议使用以下代码在运行时将目录路径添加到PATH env变量:

var dllDirectory = @"C:/some/path";
Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + ";" + dllDirectory);

That way, the change is only affecting the running process and is discarded when it exits. 这样,更改只会影响正在运行的进程,并在退出时被丢弃。

Take a look at the documentation for LoadFrom and you'll find that it says: If a native image exists for assemblyFile, it is not used. 看看LoadFrom的文档,您会发现它说: 如果assemblyFile存在本机映像,则不会使用它。 The assembly cannot be loaded as domain neutral. 无法将程序集作为域中性加载。

I believe you're going to need to add the DLL directory to the EXE's path. 我相信你需要将DLL目录添加到EXE的路径中。

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

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