根据条件编译符号更改 exe 的名称

发布于 2024-09-01 13:44:47 字数 59 浏览 4 评论 0 原文

您能否告诉 Visual Studio 根据是否设置了特定的条件编译符号来输出不同名称的 exe 文件?

Can you tell Visual Studio to output a different name of an exe file depending on if a specific conditional compilation symbol is set?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谎言月老 2024-09-08 13:44:48

这里的答案都不适合我。

它们要么产生错误,要么什么都不做。

这是我的解决方案,适用于 VS2005,我想它也适用于较新的 VS 版本。像这样编辑文件 *.csproj:

<PropertyGroup>
  <PreBuildEvent>
  </PreBuildEvent>
  <PostBuildEvent>
    if $(PlatformTarget) == x86  move /y "$(TargetPath)" "$(TargetDir)$(ProjectName)_32.exe"
    if $(PlatformTarget) == x64  move /y "$(TargetPath)" "$(TargetDir)$(ProjectName)_64.exe"
  </PostBuildEvent>
</PropertyGroup>

结果是 32 位编译生成文件 ProjectName_32.exe,64 位编译生成 ProjectName_64.exe

请注意奇怪的语法。 if 条件周围不能有括号,并且 x86 不能用引号括起来。

此方法的缺点是您无法再在调试器中启动 Exe,因为 Visual Studio 找不到它生成的 Exe。这可以通过用“复制”命令替换“移动”命令来解决,但在这种情况下,您必须将 Exe 复制到另一个目录,因为您肯定不希望同一目录中出现两次相同的文件。

这一切都是一团糟。令人难以置信的是,您可以直接在项目设置中输入输出目录,但是要执行一些非常基本的操作(例如更改 Exe 名称),您必须编写这样一个笨拙的脚本,并且具有丑陋的副作用。微软真丢脸!

None of the answers here works for me.

They either produce errors or do nothing.

Here is my solution that works in VS2005 and I suppose it will also work in newer VS versions. Edit the file *.csproj like this:

<PropertyGroup>
  <PreBuildEvent>
  </PreBuildEvent>
  <PostBuildEvent>
    if $(PlatformTarget) == x86  move /y "$(TargetPath)" "$(TargetDir)$(ProjectName)_32.exe"
    if $(PlatformTarget) == x64  move /y "$(TargetPath)" "$(TargetDir)$(ProjectName)_64.exe"
  </PostBuildEvent>
</PropertyGroup>

The result will be that a 32 bit compilation produces a file ProjectName_32.exe and a 64 bit build produces ProjectName_64.exe.

Please note the strange syntax. There must be no parenthesis around the if condition and the x86 must not be in quotes.

The disadvantage of this method is that you cannot start your Exe in the debugger anymore because Visual Studio does not find the Exe that it has generated. This could be solved by replacing the 'move' command with the 'copy' command but in this case you would have to copy the Exe to another directory because surely you don't want to have the same file twice in the same directory.

All this is a mess. It is really incredible that you can enter the output directory directly in the project settings but to do something really basic as changing the Exe name you must write such a clumsy script which has ugly side effects. Shame on Microsoft!

坚持沉默 2024-09-08 13:44:47

如果将 .csproj 文件加载到文本编辑器中,则可以控制 AssemblyName 属性:

<AssemblyName Condition="'$(Configuration)' == 'Debug'">WindowsFormsApplication9.Debug</AssemblyName>
<AssemblyName Condition="'$(Configuration)' != 'Debug'">WindowsFormsApplication9</AssemblyName>

请注意,这不仅会更改文件名,还会更改程序集名称,如果您有其他代码引用该程序集,这可能意味着麻烦。

我自己从来没有这样做过,所以我不能真正说这个想法有多好或多坏。

If you load the .csproj file into a text editor, you can control the AssemblyName property:

<AssemblyName Condition="'$(Configuration)' == 'Debug'">WindowsFormsApplication9.Debug</AssemblyName>
<AssemblyName Condition="'$(Configuration)' != 'Debug'">WindowsFormsApplication9</AssemblyName>

Note though that this does not only change the file name, but the assembly name, which might mean trouble if you have other code referencing the assembly.

I never did this myself, so I can't really say how good or bad the idea is.

你是我的挚爱i 2024-09-08 13:44:47

由于按照 Fredrik 的建议定义程序集名称标记的条件似乎会使 Visual Studio 变得暴躁,因此您可以稍后在 csproj 文件中更改程序集名称。使用 Choose element 有点像 if 语句,因此名称如果满足条件,则可以附加,如下所示。

从条件属性中的例如 DefineConstants 中获取子字符串似乎不可能(根据 MSDN)与“plain vanilla MSBuild”,但可以定义自己的构建目标并在使用 /p:Tag=value 编译时设置属性( MSBuild 命令行参考

  ...
  <Tag>true</Tag>
</PropertyGroup>
<Choose>
  <When Condition=" '$(Tag)' == 'true' ">
    <PropertyGroup>
      <AssemblyName>$(AssemblyName).TagDefined</AssemblyName>
    </PropertyGroup>
  </When>
</Choose>
<ItemGroup>
...

Since defining a condition to the assemblyname tag as suggested by Fredrik seems to make Visual Studio cranky, you can change the assembly name later in the csproj file. Using the Choose element is kind of like an if statement, so a name can be appended if a condition is met, demonstrated below.

Getting a substring out of for instance DefineConstants in a condition attribute does not seem possible (according to MSDN) with "plain vanilla MSBuild", but one can define their own build targets and set a property when compiling with a /p:Tag=value (MSBuild command line reference)

  ...
  <Tag>true</Tag>
</PropertyGroup>
<Choose>
  <When Condition=" '$(Tag)' == 'true' ">
    <PropertyGroup>
      <AssemblyName>$(AssemblyName).TagDefined</AssemblyName>
    </PropertyGroup>
  </When>
</Choose>
<ItemGroup>
...
小草泠泠 2024-09-08 13:44:47

您可以编辑 csproj 文件,它只是一个包含“任务”的 MSBuild 文件。
csproj 文件中有一个名为“AfterBuild”的部分。

也许,您可以在其中添加一个命令,将您的 exe 文件重命名为您选择的文件名。
(当然,您必须取消该部分的注释)。

也许是这样的:

<Target Name="AfterBuild">
     <Copy SourceFiles="" DestinationFiles="" Condition="" />
     <Delete Files="" Condition="" />
</Target>

我还没有进一步解决,但是您应该完成 Condition 属性,以便您可以检查条件符号是否已定义。

You can edit the csproj file, which is just an MSBuild file which contains 'tasks'.
There is a section in the csproj file which is called 'AfterBuild'.

Perhaps, you can add a command there which renames your exe file to the filename of your choice.
(Offcourse, You'll have to uncomment that section).

Perhaps something like this:

<Target Name="AfterBuild">
     <Copy SourceFiles="" DestinationFiles="" Condition="" />
     <Delete Files="" Condition="" />
</Target>

I haven't worked it out further, but you should complete the Condition attribute, so that you can check whether the conditional symbol is defined or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文