C#程序从指定路径或网络加载引用的DLL

1.在App.config 配置文件 中runtime节点下,添加privatePath属性来添加指定目录,只能填写相对路径: 

  1.  
    <runtime>
  2.  
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  3.  
    //程序集当前目录下的相对路径目录,多个相对路径用;号分割
  4.  
    <probing privatePath="目录1;目录2;" />
  5.  
    </assemblyBinding>
  6.  
    </runtime>
html

2.如果程序集是强签名后的,那么可以通过codeBase来指定网络路径或本地绝对路径。

  1.  
    <runtime>
  2.  
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  3.  
    <dependentAssembly>
  4.  
    <assemblyIdentity name="myAssembly"
  5.  
    publicKeyToken="32ab4ba45e0a69a1"
  6.  
    culture="neutral" />
  7.  
    <codeBase version="2.0.0.0"
  8.  
    href="http://www.litwareinc.com/myAssembly.dll" />
  9.  
    </dependentAssembly>
  10.  
    </assemblyBinding>
  11.  
    </runtime>
html

当然,我们还可以在代码中通过AppDomain中的几个成员来改变搜索规则,如AssemblyResolve事件、AppDomainSetup类等。

有关运行时节点的描述:https://docs.microsoft.com/zh-cn/dotnet/framework/configure-apps/file-schema/runtime/runtime-element

 

 

2026-08-17 11:36:10【出处】:https://blog.csdn.net/zhaijingkui/article/details/83652952

=======================================================================================

posted on 2026-08-17 11:36  jack_Meng  阅读(5)  评论(0)    收藏  举报

导航