将资源从dll复制到bin

时间:2015-05-11 16:52:22

标签: c#

假设我们有一个库Foo。 Foo有一些资源在构建时被复制到bin中。

假设我们有一个解决方案吧。我们在Bar中引用Foo,并且想要Foo在Bar的bin目录中拥有的资源文件。这可能吗?具体来说,这可以用可执行文件来完成吗?

1 个答案:

答案 0 :(得分:1)

至少你有几个选择。

一个是让Foo成为Bar的依赖。换句话说,将Foo项目添加到Bar的依赖项中,然后将Foo的引用属性设置为" Copy Local:True"。

对构建后发生的事情进行更多控制的另一种方法是编写自己的构建后脚本并让它在成功构建时执行。

例如,每当我构建项目Foo时,我可能希望将输出复制到部署目录。为此,我可以将以下内容添加到构建事件(项目属性 - >构建事件 - >构建后事件命令行)

$(ProjectDir)PostBuild.bat $(ProjectName) $(Platform) $(Configuration)

这将运行我的批处理文件PostBuild.bat,并包含批处理文件所期望的一些变量。批处理文件如下所示:

REM A list of all Macros --> https://msdn.microsoft.com/en-us/library/c02as0cs.aspx

@ECHO off

REM Kill the built app if we forgot to...
TSKILL %1

REM Make the target dir if it doesn't exist
IF NOT EXIST C:\DeploymentDirectory\%1 mkdir C:\DeploymentDirectory\%1

ROBOCOPY E:\repos\MyFooProject\%1\%1\bin\%2\%3\ C:\DeploymentDirectory\%1\ *.dll *.xml *.exe *.pdb *.config /purge

DEL C:\DeploymentDirectory\%1\*.vshost.*

REM Robocopy always exits with a code, which will cause 
REM VS to think something bad happened :-(
REM So the following is to handle that

if %ERRORLEVEL% GEQ 8 goto failed

rem End of batch file
GOTO success

:failed
rem Do not pause as it will pause msbuild.
exit

:success
exit 0