Change name of exe depending on conditional compilation symbol
Asked Answered
R

4

11

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

Redundant answered 18/5, 2010 at 8:30 Comment(1)
None of the methods that try to change <AssemblyName> via conditionals work if you want to make it dependent on the values of TargetFramework it seems.Cowbind
R
5

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>
...
Redundant answered 29/5, 2010 at 11:47 Comment(0)
D
13

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.

Delogu answered 18/5, 2010 at 8:36 Comment(4)
How do I enter a condition for my own symbol, such as NO_LOG or something instead of a Configuration selection?Redundant
@Patrick: my msbuild skills do not stretch quite that far. Those constants go into the DefineConstants property, but all of them go into the same property value (CONST1=value;CONST2;CONST3) and unfortunately there does not seem to be any "contains"-operator that can be used in Condition attributes.Jessy
Check this thread: social.msdn.microsoft.com/Forums/en-US/msbuild/thread/…Hautboy
Since Visual Studio seems to break down if you add a condition to assemblyname this does not work. You can however move the Assemblyname tag to the propertygroup with the defined condition...Redundant
R
5

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>
...
Redundant answered 29/5, 2010 at 11:47 Comment(0)
C
1

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.

Commanding answered 18/5, 2010 at 8:39 Comment(1)
Didn't try it, but this will nevertheless unfortunately overwrite an existing compile. Perhaps I could enter a BeforeBuild and copy the existing application if it exist. How would I enter the Condition if I have a custom conditional symbol entered in the DefineConstants tag in the csproj file?Redundant
R
1

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!

Repression answered 25/3, 2015 at 20:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.