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

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

有时候我们想让程序的exe文件和dll文件分开在不同目录,这时候可以有3种方法

1.在app.config中配置

[html]  view plain  copy
  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>  
按照引用程序集路径的不同,程序集DLL分为两类:
1)全局DLL(在GAC中注册,GAC——全局程序集缓存),有关GAC的详细资料可以参考一下链接:
http://blog.csdn.net/prince_lintb/article/details/40789211


2)私有DLL,默认情况下,添加程序集引用的时候,程序集DLL会复制到可执行目录下。
对于私有DLL,当数目众多时,由于没有分类,会显得比较凌乱。在发布软件的时候也不希望在软件安装目录下看到大量的dll文件。如果能将其分类放在不同的文件夹下就再好不过了。
问题是,如何解决应用程序对DLL的查找路径问题呢?答案是在配置文件中添加如下配置项。
<configuration>
<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <publisherPolicy apply="yes"/>
      <probing privatePath="Common;Security"/>
    </assemblyBinding>
  </runtime>
</configuration>
其中privatePath是相对于*.exe.config文件的相对路径,多个文件夹以分号分隔。
添加程序集DLL引用之后,将DLL的属性“复制本地”设置为False。程序编译过程中,会自动检索Common和Security文件夹下的DLL及其依赖项。





2. AppDomain.CurrentDomain.AppendPrivatePath来设置


3.new AppDomainSetup().PrivateBinPath 来设置

[csharp]  view plain  copy
  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.  }  


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


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

[html]  view plain  copy
  1. AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;  

[csharp]  view plain  copy
  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.         }  

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/10841660.html