【问题标题】:How can I set a file name using a variable in a Visual Studio project template如何使用 Visual Studio 项目模板中的变量设置文件名
【发布时间】:2014-11-20 10:06:37
【问题描述】:

我有一个 VS 2013 项目模板,其中包含如下项目

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

我想要的是“Target.xml”成为 MyProjectName.xml。

我知道我可以在文件中使用变量,但不能在名称中使用。

谢谢

G

【问题讨论】:

    标签: visual-studio-extensions vsix vsx visual-studio-templates


    【解决方案1】:

    确实,您也可以在项目模板中使用变量。 比如……

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

    ...其中fileinputname 是向导中指定的名称(不带扩展名)。 safeprojectname 参数将为您提供项目名称(不确定,如果它只能与项目模板一起使用,而不是在孤立的项目模板中使用)。

    这是Project and Item Templates 文档的链接;您可能感兴趣:https://msdn.microsoft.com/en-us/library/ms247121.aspx

    可以在以下位置找到可用参数列表: https://msdn.microsoft.com/en-us/library/eehb4faa.aspx

    【讨论】:

    • 似乎 TargetFileName 属性无法正常工作,当它设置时 VS 无法复制文件。我用 VS 2015 尝试了一个 C# 项目模板。
    • @tutalia 我更新了文档的链接;由于内容与 Visual Studio 2015 及更早版本相关,可能针对 Visual Studio 2017 有所更改,但我不确定...参数可能区分大小写。
    • 对于未来的读者,如果该文件在 proj 文件中被引用,不要忘记更改 proj 文件中的引用。 (即
    【解决方案2】:

    Microsoft 文档没有提到您还必须修改 .proj 文件。

    <Content Include="Target.html" />
    

    改成

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

    此外,在 .vstemplate 文件中,如果您只是更改文件名,则无需将 ReplaceParameters 属性更改为“true”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多