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?
TargetFileName. Optional attribute. Specifies the name and path of the project item when a project is created from the template. This attribute is useful for creating a directory structure different from the directory structure in the template . zip file, or for using parameter replacement to create an item name.
Project -> Export Template… Select Project Template. Select source Project. In the Template Options I choose to Automatically Import the template to Visual Studio.
This is a known issue which has not been solved by Microsoft : https://connect.microsoft.com/VisualStudio/feedback/details/565068/project-template-relative-file-path-does-not-work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With