MSDN原文:https://msdn.microsoft.com/library/twy1dw1e(v=vs.100).aspx

<runtime> 的 <assemblyBinding> 元素

.NET Framework 4             
 

包含有关程序集版本重定向和程序集位置的信息。

 
<assemblyBinding  
xmlns="urn:schemas-microsoft-com:asm.v1" appliesTo="v1.0.3705">
</assemblyBinding>

以下几节描述了属性、子元素和父元素。

特性

 

特性

说明

xmlns

必选特性。

指定程序集绑定所需的 XML 命名空间。  使用字符串“urn:schemas-microsoft-com:asm.v1”作为值。

appliesTo

指定 .NET Framework 程序集重定向所应用的运行时版本。  此可选特性使用 .NET Framework 版本号指示其适用的版本。  如果没有指定 appliesTo 特性,<assemblyBinding> 元素将适用于 .NET Framework 的所有版本。  appliesTo 特性是在 .NET Framework 1.1 版中引入的;.NET Framework 1.0 版将忽略该特性。  这意味着,即使指定了 appliesTo 特性,在使用 .NET Framework 1.0 版时所有的 <assemblyBinding> 元素也都适用。

子元素

 

元素

说明

<dependentAssembly>

封装程序集的绑定策略和程序集位置。  为每个程序集使用一个 <dependentAssembly> 标记。

<probing>

指定加载程序集时公共语言运行时搜索的子目录。

<publisherPolicy>

指定运行时是否采用出版商策略。

<qualifyAssembly>

指定当使用程序集的部分名称时应动态加载的程序集全名。

父元素

 

元素

说明

configuration

每个配置文件中的根元素,常用语言 runtime 和 .NET Framework 应用程序会使用这些文件。

runtime

包含程序集绑定和垃圾回收的相关信息。

示例

 

下面的示例显示如何将一个程序集版本重定向到另一个版本并提供基本代码。

<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="myAssembly"
publicKeyToken="32ab4ba45e0a69a1"
culture="neutral" />
<bindingRedirect oldVersion="1.0.0.0"
newVersion="2.0.0.0"/>
<codeBase version="2.0.0.0"
href="http://www.litwareinc.com/myAssembly.dll"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

下面的示例显示如何使用 appliesTo 特性重定向 .NET Framework 程序集绑定。

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" appliesTo="v1.0.3705">
<dependentAssembly>
<assemblyIdentity name="mscorcfg" publicKeyToken="b03f5f7f11d50a3a" culture=""/>
<bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="1.0.3300.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime exe文件和dll文件分开在不同目录,这时候可以有3种方法

1.在app.config中配置

  1. <runtime>
  2. <gcConcurrent enabled="true" />
  3. <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  4. <publisherPolicy apply="yes" />
  5. <probing privatePath="32;64" />
  6. </assemblyBinding>
  7. </runtime>
<runtime>
<gcConcurrent enabled="true" />
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<publisherPolicy apply="yes" />
<probing privatePath="32;64" />
</assemblyBinding>
</runtime>
2. AppDomain.CurrentDomain.AppendPrivatePath来设置

3.new AppDomainSetup().PrivateBinPath 来设置

  1. if (AppDomain.CurrentDomain.IsDefaultAppDomain())
  2. {
  3. string appName = AppDomain.CurrentDomain.FriendlyName;
  4. var currentAssembly = Assembly.GetExecutingAssembly();
  5. AppDomainSetup setup = new AppDomainSetup();
  6. setup.ApplicationBase = System.Environment.CurrentDirectory;
  7. setup.PrivateBinPath = "Libs";
  8. setup.ConfigurationFile = setup.ApplicationBase +
  9. string.Format("\\Config\\{0}.config", appName);
  10. AppDomain newDomain = AppDomain.CreateDomain("NewAppDomain", null, setup);
  11. int ret = newDomain.ExecuteAssemblyByName(currentAssembly.FullName, e.Args);
  12. AppDomain.Unload(newDomain);
  13. Environment.ExitCode = ret;
  14. Environment.Exit(0);
  15. return;
  16. }
           if (AppDomain.CurrentDomain.IsDefaultAppDomain())
{
string appName = AppDomain.CurrentDomain.FriendlyName;
var currentAssembly = Assembly.GetExecutingAssembly();
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = System.Environment.CurrentDirectory;
setup.PrivateBinPath = "Libs";
setup.ConfigurationFile = setup.ApplicationBase +
string.Format("\\Config\\{0}.config", appName);
AppDomain newDomain = AppDomain.CreateDomain("NewAppDomain", null, setup);
int ret = newDomain.ExecuteAssemblyByName(currentAssembly.FullName, e.Args);
AppDomain.Unload(newDomain);
Environment.ExitCode = ret;
Environment.Exit(0);
return;
}

可有时候又不想把他放在config文件上,只想用代码来实现,第二中方法发现已经过期,第三种方法MSDN语焉不详的,网上也没有什么资料,目前就用第四种方法

4.AppDomain有个AssemblyResolve事件,加载dll失败的时候触发,可以在这个事件里面处理

  1. AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
  1. /// <summary>
  2. /// 对外解析dll失败时调用
  3. /// </summary>
  4. /// <param name="sender"></param>
  5. /// <param name="args"></param>
  6. /// <returns></returns>
  7. static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  8. {
  9. string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Libs\");
  10. path = System.IO.Path.Combine(path, args.Name.Split(',')[0]);
  11. path = String.Format(@"{0}.dll", path);
  12. return System.Reflection.Assembly.LoadFrom(path);
  13. }
/// <summary>
/// 对外解析dll失败时调用
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
/// <returns></returns>
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Libs\");
path = System.IO.Path.Combine(path, args.Name.Split(',')[0]);
path = String.Format(@"{0}.dll", path);
return System.Reflection.Assembly.LoadFrom(path);
}

C# 自定义exe引用的dll路径的更多相关文章

  1. 【重构】C# VS 配置引用程序集的路径(分离exe和dll从指定路径调用)

    原文:[重构]C# VS 配置引用程序集的路径(分离exe和dll从指定路径调用) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/CocoWu892 ...

  2. 设置程序PrivatePath,配置引用程序集的路径(分离exe和dll)

    原文:设置程序PrivatePath,配置引用程序集的路径(分离exe和dll) 有时候我们想让程序的exe文件和dll文件分开在不同目录,这时候可以有3种方法 1.在app.config中配置 &l ...

  3. 未能找到元数据文件“引用的DLL的路径”

    使用VS的时候   偶尔会出现错误 [未能找到元数据文件“引用的DLL的路径”] 但是实际上项目中这些DLL都是做了引用的,甚至你前一天打开还是好好的,睡一觉起来 不知道什么原因 就酱紫了 原因:不详 ...

  4. C# 把引用的dll嵌入到exe文件中

    当发布的程序有引用其它dll, 又只想发布一个exe时就需要把dll打包到exe 当然有多种方法可以打包, 比如微软的ILMerge,混淆器附带的打包... 用代码打包的实现方式也有很好,本文只是其中 ...

  5. VC生成的DLL给QT的EXE调用时lib路径问题小结

    VC生成的DLL给QT调用,有两种方式,一种是隐式调用调用(使用.lib文件方式): ① 在*.pro工程文件中添加VC生成的lib文件路径时,或者使用一个绝对路径,如: LIBS += " ...

  6. .NET Winform 将引用的dll文件集成到exe中(转)

    Winform程序经常需要引用一些第三方dll文件,这些dll在发布后与exe文件保存在同一目录下,虽然将dll文件集成到exe中会增大文件尺寸,但程序目录会相对整洁. 下面介绍一种比较简单的集成方法 ...

  7. 使用ILMerge将所有引用的DLL和exe文件打成一个exe文件

    今天做了一个IM自动更新的软件,里面牵扯到了文件的解压和接口签名加密,使用了2个第三方的dll,想发布的时候才发现调用的类没几个,就像把它们都跟EXE文件打包在一起,以后复制去别的地方用也方便,于是上 ...

  8. 转载:C# 将引用的DLL文件放到指定的目录下

    当软件引用的DLL比较多的时候,全部的DLL都放在exe同目录下,显得比较乱,如果能把dll放到响应的文件夹下面,就方便很多 下面是解决该问题的一种方法: 右键点击项目:属性->设置,项目会生成 ...

  9. 以前编写的inno setup脚本,涵盖了自定义安装界面,调用dll等等应用 (转)

    以前编写的inno setup脚本,涵盖了自定义安装界面,调用dll等等应用 (转) ; Script generated by the Inno Setup 脚本向导. ; SEE THE DOCU ...

随机推荐

  1. python3爬虫全国地址信息

    PHP方式写的一团糟所以就用python3重写了一遍,所以因为第二次写了,思路也更清晰了些. 提醒:可能会有502的错误,所以做了异常以及数据库事务处理,暂时没有想到更好的优化方法,所以就先这样吧.待 ...

  2. zabbix api

    #!/usr/bin/env python # -*-coding:utf-8 -*- import requests import json class AutoZabbix: def __init ...

  3. shell-最近7天目录

    #采用将最近7天的日期放入到数组中,遍历整个目录,将这7天的目录连接成一个字符串paths. #注意: .日期目录里面的文件只是做了简单的以part开头的匹配. # .path路径是日期的上一层,以/ ...

  4. Pandas分类数据

    通常实时的数据包括重复的文本列.例如:性别,国家和代码等特征总是重复的.这些是分类数据的例子. 分类变量只能采用有限的数量,而且通常是固定的数量.除了固定长度,分类数据可能有顺序,但不能执行数字操作. ...

  5. LeetCode第[42]题(Java):Trapping Rain Water (数组方块盛水)——HARD

    题目:接雨水 难度:hard 题目内容: Given n non-negative integers representing an elevation map where the width of ...

  6. 使用 <!-- 指定使用hibernate核心配置文件 --> <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>

    在bean.xml文件中,这样使用出现问题 <!-- 指定使用hibernate核心配置文件 --> <property name="configLocations&quo ...

  7. lambda表达式----使用

    List<THealthKnowledgeEntity> tHealthKnowledgeList = tHealthKnowledgeService.queryList(query);L ...

  8. ICE 中后台开发

    1.https://alibaba.github.io/ice/#/block 2.https://www.zhihu.com/question/266529857/answer/309604282 ...

  9. 通过 HTTP 请求加载远程数据(ajax,axios)

    1.http://blog.csdn.net/liaoxiaojuan233/article/details/54176798 (Axios(JS HTTP库/Ajax库)) 2.https://ww ...

  10. 【转】安装Ubuntu时卡在logo界面

    Ubuntu15.04 安装卡在logo界面不动,进度点卡死不动,原因主要是双显卡,电源设置项默认不合理,导致安装失败. 选择USB硬盘(uefi),进入到一个有四个选项的界面,分别是 1,try U ...