Visual Studio的链接文件不存在 [英] Visual Studio Linked Files don't exist

查看:224
本文介绍了Visual Studio的链接文件不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio中,你可以做添加 - > 现有项目然后添加为链接添加下拉按钮。

In Visual Studio, you can do Add -> Existing Item and then Add as Link from the Add drop down button.

这是伟大的。这使你可以从其他项目中添加一个文件,编辑文件还编辑它在原有的项目。

This is great. This let's you add a file from another project, and editing the file also edits it in the original project.

我想使用这个功能有一个配置文件(名为Shared.config)出现在一个解决方案中的所有项目。以及有文件始终是相同的。

I would like to use this feature to have a config file (named Shared.config) be present in all projects within one solution. And have that file always be the same.

解决方案结果
|结果
| - 项目1结果
| - Shared.config [物理]结果
| - 项目2结果
| - Shared.config [链接]

solution
|
|- project 1
|- Shared.config [physical]
|- project 2
|- Shared.config [linked]

发布后,该文件确实结束了在所有已发布的项目,所以没有问题。

After publishing, the file indeed ends up in all published projects, so no problem there.

但在此之前发布(在建设开发过程中)的链接文件,并没有真正存在。想看看在Windows资源管理器的文件,证明文件不是在项目目录。 Visual Studio中只有使它看起来好像它在Solution Explorer中存在那里。
(虽然上构建,链接项目可能被复制到目录;但我不希望从斌目录。)

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 directory; But I don't want to use/access files from the bin directory.)

现在这个功能当然会出问题。要打印出 System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath(Shared.config))该项目已在发布之前会失败,因为。这一事实Shared.config并不在项目的根目录中没有

Now this gives problems off course. Trying to print out System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("Shared.config")) will fail before the project has been published, because of the fact that Shared.config doesn't exist in the project root directory yet.

我想什么做的,在这里我需要你的帮助是:

What I would like to do, and where I need your help is:


  • 我想在制作拷贝从原来的位置的所有链接的文件,以他们的目标位置。

这将会使Visual Studio中有链接的文件,原件及复印件,到都存在于具有相同名称的目录中。

This will make visual studio have the linked file, and a copy of the original, to exists both in the same directory with the same name.

通常情况下,VS不会让你,如果该目录已经包含具有相同名称的文件创建一个目录链接项。

Normally , VS won't allow you to create a linked item in a directory if that directory already contains a file with the same name.

不过, ,我首先创建一个链接项测试;然后使用Windows资源管理器复制原始文件到目标目录,请参见Visual Studio行为确定。该解决方案资源管理器只是隐藏物理文件,并显示在代替链接的项目。 (即使你单击显示在Solution Explorer中的所有文件

But, I have tested by creating a linked item first; then using Windows Explorer to copy the original file to the destination directory, and see Visual Studio act ok. The solution explorer simply hides the physical file, and shows the linked item in stead. (Even if you click Show all files in the solution explorer.)

解决方案结果
|结果
| - 项目1结果
| - Shared.config [物理]结果
| - 项目2个结果
| - Shared.config [链接]结果
| - Shared.config [物理,构建过程中复制在这里,看不到解决方案资源管理]

solution
|
|- project 1
|- Shared.config [physical]
|- project 2
|- Shared.config [linked]
|- Shared.config [physical, copied here during build, invisible to Solution explorer]

这正是我想要的!当试图编辑文件,Visual Studio将打开链接项。并构建一个物理文件将因此存在试图访问它的代码复制到目标目录。

This is exactly what I want! When trying to edit the file, Visual Studio will open the 'linked item'. And on build, a physical file will be copied to the target directory so it exists for the code that tries to access it.

现在我该怎么办呢?
如果这与生成事件做了什么?如果让我怎么说'所有链接的文件的副本原件到目的地目录?

Now how do I do this? Should this be done with Build events? If so how do I say 'copy originals of all linked files to their destination directory?

推荐答案

您应该使用的MSBuild功能来实现这一点。

You should probably use MSBuild features to implement this.

编辑的csproj文件
(在Visual Studio中,右键单击该项目,并卸载它。然后右键单击并编辑)

Edit the csproj file (in Visual Studio, right click the project and unload it. then right click and edit)

滚动至底部,您应该找到这一行

Scroll to the bottom and you should find this line.

<导入项目=$( 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>

现在每次生成时,会发生生成操作权之前,的MSBuild将复制所有链接的文件。

Now every time you build, right before the build action occurs, MSBuild will copy all linked files.

说明

的ItemGroup 包含我的数组命名为LINKEDITEM。我生成这个数组通过添加包含一个链接属性只有无项目。

ItemGroup contains my "array" named "LinkedItem". I generate this array by adding only the "None" items that contain a link property.

目标是一个MSBuild概念。你可以把它作为构建的特定阶段。我命名这个阶段CopyLinkedFiles,但你可以使用任何名字。

Target is an MSBuild concept. You can think of it as a particular phase of the build. I named this phase "CopyLinkedFiles" but you can name it anything.

BeforeTargets 是一个指令,它告诉的MSBuild来运行指定阶段之前的动作。在这里,我选择了建设阶段之前运行CopyLinkedFiles。

BeforeTargets is a directive that tells MSBuild to run the action before the specified phase. Here, I have chosen to run "CopyLinkedFiles" before the "Build" phase.

输入是一种优化的参数。使用它,如果没有必要跳过副本,加快建设。您可以忽略此参数,如果你不在乎。的MSBuild的输入比较预期的输出时间戳,看它是否需要执行。

Inputs is an optimization parameter. It is used to speed up building by skipping the copy if not necessary. You can ignore this parameter if you don't care. MSBuild compares the Inputs to the expected Outputs timestamp to see if it needs to execute.

复制是接受复制的文件输出到指定的文件夹中的MSBuild任务。

Copy is an MSBuild task that accepts a file to copy and outputs to the specified folder.

减少冗余

您可以粘贴到每一个文件的.csproj,或者你可以把它放在一个中央.proj和嵌入该进的csproj文件中。可悲的是,不管你做什么,你必须编辑每一个.csproj的至少1次。 (

You could paste this into every .csproj file, or you could put it in a central .proj and embed that into the csproj files. Sadly, no matter what you do, you will have to edit every .csproj at least 1 time. :(

创建通用项目通话 WhateverNameYouLike.proj
文件把这些内容写入。文件

Create a file in the Common project call WhateverNameYouLike.proj Put these contents into the file.

<?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>

现在恼人的一部分:
在每一个.csproj的,你将不得不添加一行
<导入项目=.. \Common \WhateverNameYouLike.proj/>
可能在年底之前的< /项目方式> 结束标记

Now the annoying part: In every .csproj, you will have to add a line like <Import Project="..\Common\WhateverNameYouLike.proj" /> probably at the end just before the </Project> closing tag.

这篇关于Visual Studio的链接文件不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆