有时候我们想让程序的exe文件和dll文件分开在不同目录,这时候可以有3种方法
1.在app.config中配置
- <runtime>
- <gcConcurrent enabled="true" />
- <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
- <publisherPolicy apply="yes" />
- <probing privatePath="32;64" />
- </assemblyBinding>
- </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及其依赖项。
3.new AppDomainSetup().PrivateBinPath 来设置
- 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失败的时候触发,可以在这个事件里面处理
- AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
- /// <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);
- }