• WPF: 把引用的dll移动到自定义路径


    需求:

    有A.exe和B.exe, 都引用了 C.dll, output路径都是 W:Debug.
    A和B都添加了对C的引用,正常情况下C会被复制到 output 里面。
    C这样子的dll很多,不想把它们和exe放在同一级的目录,移动到子目录,如W:Debug3rdDll

    办法:

    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)3rdDllc.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 + @"3rdDllF.dll";
               return Assembly.LoadFrom(dllPath);
           }

    Over!

  • 相关阅读:
    性能测试总结(一)测试流程
    WSDL入门
    Python中的while循环和for循环
    python中的变量
    吴恩达《机器学习》章节2单变量线性回归
    吴恩达《机器学习》章节1绪论:初识机器学习
    新式类多继承的查找顺序
    python2x和python3x的一些区别
    类方法和静态方法
    @property的使用
  • 原文地址:https://www.cnblogs.com/xiaokang088/p/3209003.html
Copyright © 2020-2023  润新知