You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio的.vstemplate模板用$projectname$参数时文件不复制问题

核心问题原因

你当前配置中,需要使用$projectname$参数的ProjectItem条目ReplaceParameters设为了false,VS不会解析TargetFileName字段中的模板参数,无法生成合法文件名,直接跳过了这类文件的复制流程。

修复步骤

  • 调整MyTemplate.vstemplate配置
    所有需要使用$projectname$重命名的文件,对应的ProjectItem必须将ReplaceParameters设为true,示例配置如下:
<TemplateContent>
  <Project File="Minimal.vcxproj" ReplaceParameters="true">
    <!-- 重命名cpp文件 -->
    <ProjectItem ReplaceParameters="true" TargetFileName="$projectname$.cpp">Minimal.cpp</ProjectItem>
    <!-- 重命名rc文件 -->
    <ProjectItem ReplaceParameters="true" TargetFileName="$projectname$.rc">Minimal.rc</ProjectItem>
    <!-- 无需重命名的resource.h保持原有配置 -->
    <ProjectItem ReplaceParameters="false">resource.h</ProjectItem>
    <!-- 原有正常工作的项目文件配置保持即可 -->
    <ProjectItem ReplaceParameters="true" TargetFileName="$projectname$.vcxproj">Minimal.vcxproj</ProjectItem>
    <ProjectItem ReplaceParameters="true" TargetFileName="$projectname$.vcxproj.filters">Minimal.vcxproj.filters</ProjectItem>
  </Project>
</TemplateContent>

如果不需要替换.rc/.cpp文件内部的内容,只需要重命名文件名,只要保证文件内部没有$开头的内容即可,若文件原有内容包含$,将其改写为$$即可避免被VS误识别为模板参数替换。

  • 同步修改项目文件中的引用
    打开压缩包内的Minimal.vcxproj,将原有的文件引用部分替换为模板参数:
<ItemGroup>
  <ClCompile Include="$projectname$.cpp" />
  <ResourceCompile Include="$projectname$.rc" />
</ItemGroup>

同时修改Minimal.vcxproj.filters中的对应引用,避免创建项目后文件不显示在解决方案管理器中。

  • 生效配置
    将所有修改后的文件重新打包为zip,替换原有模板目录下的压缩包,重启Visual Studio后测试即可。

内容的提问来源于stack exchange,提问作者Rusty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 14:39:01