nl-*_*l-x 18 c# visual-studio-2010 build-events
在Visual Studio中,您可以执行Add
- > Existing Item
然后Add as Link
从Add
下拉按钮.
这很棒.这是让你从另一个项目中添加一个文件,编辑文件也会在原始项目中编辑它.
我想使用此功能在一个解决方案中的所有项目中都有一个配置文件(名为Shared.config).并且该文件始终相同.
解决方案
|
| - 项目1
|
- Shared.config [physical]
|- project 2
|
- Shared.config [linked]
After publishing, the file indeed ends up in all published projects, so no problem there.
But BEFORE publishing (during development on build) the linked file doesn't really exist. Trying to see the file in the Windows Explorer proves that the file is not in the project directory. Visual Studio only makes it look as if it exists there in the solution explorer.
(Though on build, linked items are probably copied to the bin
目录;但我不想使用/访问bin
目录中的文件.)
现在这会让问题偏离正轨.System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("Shared.config"))
在项目发布之前,尝试打印将失败,因为项目根目录中尚不存在Shared.config.
我想做什么,以及我需要你帮助的地方是:
这将使visual studio具有链接文件和原始文件的副本,以便在同一目录中存在同名.
通常,如果该目录已包含具有相同名称的文件,则VS将不允许您在目录中创建链接项.
但是,我已经通过首先创建一个链接项来测试; 然后使用Windows资源管理器将原始文件复制到目标目录,并查看Visual Studio的行为.解决方案资源管理器只隐藏物理文件,并显示链接项.(即使您单击Show all files
解决方案资源管理器.)
解决方案
|
| - 项目1
|
- Shared.config [physical]
|- project 2
|
- Shared.config [linked]
|
- Shared.config [物理,在构建期间复制到此处,对于解决方案资源管理器不可见]
这正是我想要的!尝试编辑文件时,Visual Studio将打开"链接项".在构建时,物理文件将被复制到目标目录,因此它存在于尝试访问它的代码中.
现在我该怎么做?是否应该使用Build事件完成?如果是这样,我怎么说'将所有链接文件的原件复制到目标目录?
lor*_*ond 24
我在dss539的答案中遇到了一些麻烦(在vs2010中测试过).首先,对于每个匹配的文件,在解决方案资源管理器中出现重复项.其次,'%link'
它不是属性访问器,它是字符串本身,它始终不等于空字符串''
,因此每个具有None
构建操作的文件都在构建时进行匹配,复制和复制.第三,链接文件正在复制到项目的根目录中,但我需要复制放置链接的文件.所以我做了一些修改:
<Target Name="CopyLinkedFiles" BeforeTargets="Build">
<ItemGroup>
<LinkedItem Include="@(Content)" Condition="%(Content.Link) != ''" />
</ItemGroup>
<Copy SourceFiles="@(LinkedItem)" DestinationFiles="%(LinkedItem.Link)"/>
</Target>
Run Code Online (Sandbox Code Playgroud)
LinkedItem
不再是解决方案资源管理器中的重复项.要创建要复制的链接,您必须设置Content
构建操作.
dss*_*539 15
您应该使用MSBuild功能来实现此功能.
编辑csproj文件(在Visual Studio中,右键单击项目并卸载它.然后右键单击并编辑)
滚动到底部,你应该找到这一行.
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
在该行之后,立即添加这些行.
<ItemGroup>
<LinkedItem Include="@(None)" Condition="'%Link' != ''" />
</ItemGroup>
<Target Name="CopyLinkedFiles" BeforeTargets="Build" Inputs="@(LinkedItem)" Outputs="@(LinkedItem->'%(Filename)%(Extension)')">
<Copy SourceFiles="@(LinkedItem)" DestinationFolder="$(MSBuildProjectDirectory)" />
</Target>
Run Code Online (Sandbox Code Playgroud)
现在每次构建时,就在构建操作发生之前,MSBuild将复制所有链接的文件.
说明
ItemGroup
包含名为"LinkedItem"的"数组".我通过仅添加包含link属性的"None"项来生成此数组.
Target
是一个MSBuild概念.您可以将其视为构建的特定阶段.我将此阶段命名为"CopyLinkedFiles",但您可以将其命名为任何名称.
BeforeTargets
告诉MSBuild在指定阶段之前运行操作的指令.在这里,我选择在"构建"阶段之前运行"CopyLinkedFiles".
Inputs
是一个优化参数.如果没有必要,它可以通过跳过副本来加速构建.如果您不在乎,可以忽略此参数.MSBuild将Inputs
预期Outputs
时间戳与预期时间戳进行比较,以确定是否需要执行.
Copy
是一个MSBuild任务,它接受要复制并输出到指定文件夹的文件.
减少冗余
您可以将其粘贴到每个.csproj文件中,或者您可以将其放在中心.proj中并将其嵌入到csproj文件中.可悲的是,无论你做什么,你都必须至少编辑一次.csproj.:(
在Common项目调用中创建一个文件WhateverNameYouLike.proj
将这些内容放入文件中.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- paste the previously shown code here -->
<!-- you can save yourself some hassle by linking the config file here, but if you really enjoy adding the file as a link to every project, you can skip this line -->
<None Include="..\Common\Shared.config">
<Link>Shared.config</Link>
</None>
</Project>
Run Code Online (Sandbox Code Playgroud)
现在讨厌的部分:在每个.csproj中,你必须<Import Project="..\Common\WhateverNameYouLike.proj" />
在</Project>
结束标记之前的末尾添加一行
.
归档时间: |
|
查看次数: |
12501 次 |
最近记录: |