csharp添加引用路径_WPF: 把引用的dll移动到自定义路径

本文介绍了如何在C#(WPF)项目中将引用的DLL移动到自定义路径,避免与EXE在同一目录。通过设置Copy Local属性为False,创建PostBuild事件移动DLL,并使用AppDomain.AppendPrivatePath或AssemblyResolve事件来加载自定义路径中的DLL。
摘要由CSDN通过智能技术生成

需求:

有A.exe和B.exe, 都引用了 C.dll, output路径都是 W:\Debug.

A和B都添加了对C的引用,正常情况下C会被复制到 output 里面。

C这样子的dll很多,不想把它们和exe放在同一级的目录,移动到子目录,如W:\Debug\3rdDll

办法:

1. 首先设置C.dll

打开Project A的References,选中C.dll, 右键Properties,Copy Local 设为False,这个dll就不会拷贝了。

2. 设置PostBuild,

打开B的Build Events,  PostBuild 输入

mkdir  $(OutputPath)\3rdDll

move $(OutputPath)\c.dll  $(OutputPath)\3rdDll\c.dll

A和B可以互换。

3. A和B 加入Config文件,

Config:  Build Action=None, Copy to Output Directory=Do not copy

或者 app.xaml.cs里面加入:

public App()

{

this.Startup += new StartupEventHandler(App_Startup);

}

void App_Startup(object sender, StartupEventArgs e)

{

var name = Assembly.GetExecutingAssembly().Location;

var path = Path.GetDirectoryName(name);

AppDomain.CurrentDomain.AppendPrivatePath(path+ @"\3rdDll");

}

AppDomain.CurrentDomain.AppendPrivatePath 貌似过时了,AppDomainSetup 的使用方法没找到。

注意:3rdDll这个文件夹必须在 W:\Debug 的子目录或者更深一级的目录。

特殊需求:

F.dll 不在 W:\Debug 的子目录或者更深一级的目录,例如上一级目录;

或者exe的目录级别比dll的低一些,

用这个办法:

public App()

{

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

}

System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)

{

var name = Assembly.GetExecutingAssembly().Location;

var path = Path.GetDirectoryName(Path.GetDirectoryName(name));

var dllPath = path + @"\3rdDll\F.dll";

return Assembly.LoadFrom(dllPath);

}

Over!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值