Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2012 - How to add a file link to a file in the same project

When I add a file from another Visual Studio 2012 project to my current project by using the 'Add As Link' option from the 'Add Existing Item' dialog. The file link is nicely created and everything is fine.

When I repeat the same trick for an existing file in the same VS2012 project (but located in another folder) the link won't be created. Visual Studio just ignores the action. Why isn't this possible? Is there a workaround for this, so I can add file links to files in the same project?

Background information: I want to use NuGet packages and its update mechanism, but NuGet just dumps everything in a folder structure (like the 'Scripts' and 'Content' folders), which I don't want to use. I use a 'libs' folder for external stuff. But when I move the NuGet imported files, it breaks the update mechanism, so I want to create file links (below the libs folder) to the imported files located in the 'Scripts' folder etc.

I already tried to manually edit the .csproj file, but than the link definitions are still ignored. The only solution I can think of is by creating another project for just the NuGet packages and add links to those imported files in my other project, but that seems to me as a bit of overkill.

like image 355
Maarten Docter Avatar asked Mar 08 '13 08:03

Maarten Docter


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.


2 Answers

This is not possible.

The csproj file is simply an msbuild file, telling the build process what files and references make up the project and what to do in terms of build etc.

If you look at the way the csproj file is put together, you'll notice that there are ItemGroup elements. One of the functions of these ItemGroup elements is to group together files. In a "file group", each item (whether marked as Compile or None or whatever) refers to a file with the Include attribute e.g. Include="filename.ext". When you add a file to the solution, it will create one of these elements with the Include attribute appropriately set to the path of the file relative to the project. Before it does that though, it checks to see whether a file with the matching path is already in the project, and ignores it if it is.

So you see, what you're trying to do is add a duplicate file, and VS is not allowing that.

--Edit--

Now, as for a work-around. If you were to use a symlink, this can be done. In the command window, type the following command:

mklink Link Target

where Link specifies the new symbolic link name and Target specifies the path (relative or absolute) that the new link refers to.

You should then be able to add the file to your project as required.

like image 148
CarllDev Avatar answered Oct 06 '22 00:10

CarllDev


You can edit .csproj file. For example, replace

<Content Include="fonts\fontawesome-webfont.woff" />

by

<Content Include="fonts\fontawesome-webfont.woff" >
   <Link>newPath\fonts\fontawesome-webfont.woff</Link>
</Content>

New path will be hidden in solution explorer, but it will be copied (tested in VS2015 CTP6)

like image 29
RouR Avatar answered Oct 05 '22 23:10

RouR