C# 在 Visual Studio 上将文件添加为链接 - 调试与发布

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18963750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:43:11  来源:igfitidea点击:

Add File as a Link on Visual Studio - Debug vs Publish

c#asp.netvisual-studio

提问by Nuno

Every time I have to link a file into an ASP.NET project as link, there is always something I forget to do, because Visual Studio is way too buggy and annoying with these. There is a bug/limitation with it so it does not copy the file into the Debug output folder, and we must change the properties of the file so that it is copied into the Publish output folder as well.

每次我必须将文件作为链接链接到 ASP.NET 项目时,总会有一些我忘记做的事情,因为 Visual Studio 的问题太多,而且很烦人。它有一个错误/限制,因此它不会将文件复制到 Debug 输出文件夹中,我们必须更改文件的属性,以便将其也复制到 Publish 输出文件夹中。

I learned this in the hard way, after a lot of research, and I created an AfterBuild target to help in making this process easier. However, there is still some manual work to do when adding the file.

经过大量研究后,我艰难地了解到了这一点,并创建了一个 AfterBuild 目标来帮助简化此过程。但是,在添加文件时仍有一些手动工作要做。

Here is an example for a project I maintain. Say we want to add the file YIUCARD in the “utilities” folder of the ASP.NET project.

这是我维护的项目的示例。假设我们要在 ASP.NET 项目的“utilities”文件夹中添加文件 YIUCARD。

1) Right-click “utilities” folder on Visual Studio and select “Add -> Existing item”.

1) 右键单击​​ Visual Studio 上的“utilities”文件夹,然后选择“添加 -> 现有项目”。

Step 1

第1步

2) Select the file to add, clicking on it ONCE (do not double-click).

2) 选择要添加的文件,单击一次(不要双击)。

Step 2

第2步

3) Click in the arrow next to the “Add” button and select “Add As Link”.

3) 单击“添加”按钮旁边的箭头并选择“添加为链接”。

Step 3

第 3 步

At the moment, this will do nothing. The file won't be copied to any folder (Debug and Publish). It is just added to the project.

目前,这不会有任何作用。该文件不会被复制到任何文件夹(调试和发布)。它只是添加到项目中。

4) Right-click in the file and open Properties.

4) 右键单击​​该文件并打开“属性”。

Step 4

第四步

5) Change: “Build Action” to “Content” “Copy to Output Directory” to “Copy always”

5)更改:“构建操作”到“内容”“复制到输出目录”到“始终复制”

Step 5

第 5 步

At this time, the file will be copied into the Publish output folder (anywhere we define when publishing). However, when we debug locally (F5), it won't work because the file is not copied into the local “utilities” folder on the codebase.

这时候文件会被复制到Publish输出文件夹(我们在发布时定义的任何地方)。然而,当我们在本地调试(F5)时,它不会工作,因为文件没有被复制到代码库的本地“utilities”文件夹中。

6) Save the changes on Visual Studio, by selecting “Save All”. This is so that the “.csproj” file is saved, as we made changes on it and we will now edit it manually.

6) 通过选择“全部保存”,在 Visual Studio 上保存更改。这是为了保存“.csproj”文件,因为我们对其进行了更改,现在我们将手动编辑它。

Step 6

第 6 步

7) Open the “.csproj” in Notepad++.

7) 在 Notepad++ 中打开“.csproj”。

Step 7

第 7 步

8) Add the file to the "BeforeBuild" event (here's mine):

8)将文件添加到“BeforeBuild”事件(这是我的):

Step 8

第 8 步

 <Target Name="BeforeBuild">
    <ItemGroup>
      <UtilitiesFiles Include="..\path\to\file\REDOPXX.NEW" />
      <UtilitiesFiles Include="..\path\to\file\REDFMTST" />
      <UtilitiesFiles Include="..\path\to\file\JOBCARD" />
      <UtilitiesFiles Include="..\path\to\file\YIUCARD" />
    </ItemGroup>
    <Copy SourceFiles="@(UtilitiesFiles)" DestinationFolder=".\utilities" SkipUnchangedFiles="true" />
  </Target>

9) I have an "AfterBuild" event for Release mode that automatically publishes the project for me, so that the files go directly to the output folder I want:

9) 我有一个用于发布模式的“AfterBuild”事件,它会自动为我发布项目,以便文件直接转到我想要的输出文件夹:

  <Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release' ">
    <PropertyGroup>
      <OutputDir>..\path\to\publish\MyProject</OutputDir>
    </PropertyGroup>
    <Message Importance="High" Text="Output DIR: $(OutputDir)" />
    <RemoveDir Directories="$(OutputDir)" ContinueOnError="true" />
    <MSBuild Projects="MyProject.csproj" Properties="Configuration=$(Configuration);WebProjectOutputDir=$(OutputDir);OutDir=$(OutputDir)bin\" Targets="ResolveReferences;_CopyWebApplication" />
  </Target>

10) Save the file in Notepad++ and reload it on Visual Studio.

10) 将文件保存在 Notepad++ 中并在 Visual Studio 中重新加载。

Step 10

第 10 步

When you build the project, the file will be copied to both Debug and Release/Publish folders!

当您构建项目时,该文件将被复制到 Debug 和 Release/Publish 文件夹!

However, I would like to know if there is any easier/intuitive way to do this.

但是,我想知道是否有任何更简单/直观的方法来做到这一点。

You may say that this would be fixed by changing the Debug output folder to the same as the Release/Publish folder, so that the files would be there after the first time I published. However, please note that there is a bug with this, as well. If I use an output folder other than the "bin\", aspx files will complain that they can't find the assemblies. Even if I set "Copy to Output Directory" to "Copy Always", it will complain that "Global.asax.cs" could not be found.

您可能会说,这可以通过将 Debug 输出文件夹更改为与 Release/Publish 文件夹相同来解决,这样文件在我第一次发布后就会在那里。但是,请注意,这也有一个错误。如果我使用“bin\”以外的输出文件夹,aspx 文件会抱怨他们找不到程序集。即使我将“复制到输出目录”设置为“始终复制”,它也会抱怨找不到“Global.asax.cs”。

Please see these related questions:

请参阅以下相关问题:

Parser Error Message: Could not load type 'TestMvcApplication.MvcApplication'

解析器错误消息:无法加载类型“TestMvcApplication.MvcApplication”

"Could not load type [Namespace].Global" causing me grief

“无法加载类型 [Namespace].Global”让我感到悲伤

Is this fixed/improved in the newer versions of Visual Studio?

这是否在较新版本的 Visual Studio 中得到修复/改进?

回答by Keith

If the goal is to include content folders (e.g. scripts, images), then you should not manage the individual files. Instead, include these folders in your build and publish files.

如果目标是包含内容文件夹(例如脚本、图像),则不应管理单个文件。相反,将这些文件夹包含在您的构建和发布文件中。

When files need to be added/removed, simply update them in the content folder. This is essentially how we handle Help files since they are not managed by the developers.

当需要添加/删除文件时,只需在内容文件夹中更新它们。这基本上就是我们处理帮助文件的方式,因为它们不是由开发人员管理的。

Check out these links:

查看这些链接:

Is there a way to automatically include content files into asp.net project file?

有没有办法自动将内容文件包含到 asp.net 项目文件中?

ASP.NET Web Deployment using Visual Studio: Deploying Extra Files

使用 Visual Studio 的 ASP.NET Web 部署:部署额外文件

回答by Dejan

The MSBuild.WebApplication.CopyContentLinkedFilesNuGet might help you. This package adds a MSBuild target which copies all content files added as link to the Web Application folder during the build.

MSBuild.WebApplication.CopyContentLinkedFiles的NuGet可以帮助你。此包添加了一个 MSBuild 目标,该目标将在构建期间作为链接添加到 Web 应用程序文件夹的所有内容文件复制。

回答by jornathan

I think one of best way is make a symbolic link as you want. It will helpful to you to manage file that always must be same.

我认为最好的方法之一是根据需要制作符号链接。管理始终必须相同的文件对您很有帮助。