Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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.

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.

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

Step 1

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

Step 2

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

Step 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.

Step 4

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

Step 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.

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.

Step 6

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

Step 7

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

Step 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:

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

Step 10

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

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.

Please see these related questions:

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

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

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

like image 821
Nuno Avatar asked Sep 23 '13 15:09

Nuno


People also ask

How do I add a link to a file in Visual Studio?

To add a file as a link, right click and choose Add > Existing Item… as before, but this time, don't click the Add button. Instead, click the little dropdown arrow next to the Add button and select Add as Link. Instead of copying the file into the project directory, Visual Studio will create a link to the original.

How do I link a file to a project in Visual Studio?

1) Right-click “utilities” folder on Visual Studio and select “Add -> Existing item”. 2) Select the file to add, clicking on it ONCE (do not double-click). 3) Click in the arrow next to the “Add” button and select “Add As Link”.

How do I add a file path in Visual Studio?

In Visual Studio, click Tools > Options. Expand Projects and Solutions and click Locations. The Projects location field defines the default location for storing new projects. You can change this path if you are using a different working folder.

What is publish in Visual Studio?

How does Publish in Visual Studio Work? In simple terms, Publishing creates the set of files that are needed to run your application, and you can deploy the files by copying them to a target machine.


1 Answers

The MSBuild.WebApplication.CopyContentLinkedFiles NuGet 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.

like image 142
Dejan Avatar answered Sep 21 '22 20:09

Dejan