如何在 VS2015 中更改特定配置的可执行文件名称

How to change executable name for a specific configuration in VS2015

我需要根据使用的配置动态更改已编译项目(在 VS 2015 中)的可执行文件名称。 例如。在发布模式下,结果必须是 release.exe,在调试模式下 - debug.exe.

我知道我可以在 .csproj 中更改 <AssemblyName>,但问题是我必须保持程序集名称不变。

I know that I can change in .csproj but the problem is that I must leave assembly name unchanged.

由于您必须保持程序集名称不变,您可以尝试将复制目标添加到您的项目中以重命名可执行文件名称。

为此,请卸载您的项目。然后在项目的最后,就在 end-tag </project> 之前,将脚本放在下面:

  <Target Name="AfterBuild">
    <Copy SourceFiles="$(TargetPath)" DestinationFiles="$(TargetDir)$(Configuration).exe" Condition="'$(Configuration)' == 'Debug'"/>
    <Delete Files="$(TargetPath)" Condition="'$(Configuration)' == 'Debug'"/>
  </Target>

如果您还想重命名输出中的其他文件,您可以将自定义目标更改为:

<Target Name="AfterBuild">
    <Copy SourceFiles="$(TargetDir)$(AssemblyName).exe" DestinationFiles="$(TargetDir)$(Configuration).exe" Condition="'$(Configuration)' == 'Debug'"/>
    <Copy SourceFiles="$(TargetDir)$(AssemblyName).exe.config" DestinationFiles="$(TargetDir)$(Configuration).exe.config" Condition="'$(Configuration)' == 'Debug'"/>
    <Copy SourceFiles="$(TargetDir)$(AssemblyName).pdb" DestinationFiles="$(TargetDir)$(Configuration).pdb" Condition="'$(Configuration)' == 'Debug'"/>
    <Delete Files="$(TargetDir)$(AssemblyName).exe" Condition="'$(Configuration)' == 'Debug'"/>
    <Delete Files="$(TargetDir)$(AssemblyName).exe.config" Condition="'$(Configuration)' == 'Debug'"/>
    <Delete Files="$(TargetDir)$(AssemblyName).pdb" Condition="'$(Configuration)' == 'Debug'"/>
</Target>

希望对您有所帮助。