Can I use TargetFileName to place a file up a directory within a Visual Studio Template?

Viewed 2032

I have a .cs file with a base class that I want to use from within many projects. I've first placed this cs file above the project folder (along side the .sln). Then, within the project, I've changed the

<Compile Include="BaseClass.cs">
to
<Compile Include="..\BaseClass.cs">

This works, and the file icon then has a little shortcut arrow.


The problem comes when I want to export the project as a project template. The BaseClass does not get included in the template because it is outside the project folder. I've tried adding the BaseClass.cs to the template zip and adding
<ProjectItem ReplaceParameters="true" TargetFileName="..\BaseClass.cs">BaseClass.cs</ProjectItem>
to the MyTemplate.vstemplate, but I get the error:
A target file name within the VSTemplate file is invalid, it contains a fully qualified path
If I remove the ..\, it includes the file directly. If I leave the <ProjectItem> out of the .vstemplate file, and just put <Compile Include="..\BaseClass.cs"> in the .csproj file in the template, it looks for the file in Users/me/AppData/Local....

How can I get the template to reference a file outside the project directory?

2 Answers

You should be doing the mapping of the folders in the VSTemplate and not in the project. The VS would not do any manipulation but in the template.

An example would be something like this. The below would copy the file from the "InnerFolder" to the root.

<Folder Name="InnerFolder" TargetFolderName="." >
    <ProjectItem ReplaceParameters="true" TargetFileName="Web.config">Web.config</ProjectItem>
</Folder>
Related