How can I set a file name using a variable in a Visual Studio project template

5.6k views Asked by At

I have a VS 2013 project template with a item like below

<ProjectItem ReplaceParameters="true" TargetFileName="Target.xml">Target.xml</ProjectItem>

What I want is "Target.xml" to be MyProjectName.xml.

I know I can use variables inside the file but not in the name.

Thanks

G

2

There are 2 answers

0
jlo-gmail On

The Microsoft documentation doesn't mention that you also have to modify the .proj file.

<Content Include="Target.html" />

change to

<Content Include="$projectname$.html" />

Also, in the .vstemplate file, the ReplaceParameters attribute doesn't need to be changed to "true" if you're just changing the filename.

3
Matze On

Indeed, you can also use variables within the item template. For instance...

<ProjectItem 
    TargetFileName="$fileinputname$.xml" 
    ReplaceParameters="true" 
    ItemType="Content">Target.xml</ProjectItem>

...whereby fileinputname is the name as specified in the wizard (without extension). The safeprojectname parameter would give you the project name (not sure, if this can only be used in conjunction with a project template, rather than in an isolated item template).

This is the link to the Project and Item Templates documentation; might be of your interest: https://msdn.microsoft.com/en-us/library/ms247121.aspx

A list of available parameters can be found at: https://msdn.microsoft.com/en-us/library/eehb4faa.aspx