需求:
有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
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="3rdDll;"/>
</assemblyBinding>
</runtime>
</configuration>
或者 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!
