使用dll的相对路径构建项目

时间:2016-09-23 01:35:06

标签: c# visual-studio-2013 relative-path buildconfiguration dll-reference

我在其中创建了一个c#项目,我引用了system.windows.interactivity.dll 我想知道的是如何设置项目,以便在构建* .exe时我得到这种结构:

Program Folder
    program.exe
Libraries Folder
    system.windows.interactivity.dll

我通过在解决方案文件夹下放置一个“Libraries”文件夹尝试了一些实验,以便它与项目文件夹处于同一级别。这给出了csproj文件中“... \ Libraries \ system.windows.interactivity.dll”的相对路径,但这不是解决方案,因为当我编译它时将dll复制到带有exe的调试文件夹中,它保留了这个“相同级别”的路径结构。

如何更改内容以便将dll放在另一个目录中并将其引用?

[更新]
所以我在我的项目中修改了以下内容:
1:将参考system.windows.interactivity.dll上的“复制本地”属性更改为False 2:在csproj文件中添加以下代码,以检查输出目录上是否存在Libraries文件夹,如果不是,则创建然后复制dll。

  <Target Name="BeforeBuild">
    <MakeDir Directories="$(OutDir)..\Libraries" 
             Condition="!Exists('$(OutDir)..\Libraries')" />
    <Copy SourceFiles="..\Libraries\System.Windows.Interactivity.dll" 
          DestinationFolder="$(OutDir)..\Libraries" 
          ContinueOnError="True" />
  </Target>

3。将以下代码添加到App.config中,为应用程序添加另一个位置以搜索dll。

<configuration>
    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <probing privatePath="..\Libraries"/>
      </assemblyBinding>
    </runtime>
</configuration>

我的发现:
在构建应用程序时,所有文件都正是我想要的位置,就像我原始帖子中的结构一样。当我尝试从输出目录运行exe时,它找不到dll。

[/更新]

1 个答案:

答案 0 :(得分:1)

使用app.config可以告诉.NET探测子目录以搜索程序集(.NET需要知道当应用程序运行在非标准位置时如何查找DLL):

https://msdn.microsoft.com/en-us/library/823z9h8w(v=vs.110).aspx

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <probing privatePath="bin;bin2\subbin;bin3"/>
      </assemblyBinding>
   </runtime>
</configuration>

您可以修改.csproj文件以执行BeforeBuild / AfterBuild任务并将dll复制到子目录。但是,如果您真的只希望使用此结构进行部署,则可能更容易将其作为包/安装程序逻辑的一部分而不是直接构建逻辑包含在内。通常,让编译器为您的DLL选择输出目标要容易得多。

以下是您如何创建BeforeBuild复制任务的示例:

Copy all files and folders using msbuild

您可以通过在引用上将“Copy Local”设置为false来告诉Visual Studio不要将DLL复制到输出文件夹。

相关问题