Visual Studio post-build event macros are empty
Asked Answered
I

6

26

In Visual Studio 2013 (or 2015), I have a particular project that in the post-build event, I'm trying to pass $(TargetDir) to a batch file.

The problem is, all the macros are empty, except for the macros beginning in "Solution". Also, all the macros are shown correctly in the "Edit Post-build Event Command line" dialog.

Example

echo ConfigurationName is "$(ConfigurationName)"

Results in

Configuration name is ""
Ichabod answered 5/11, 2014 at 2:39 Comment(0)
W
40

I had the same problem. According to https://social.msdn.microsoft.com/Forums/vstudio/en-US/179716e8-89eb-40ff-ba13-339e2d25d1c8/outdir-and-targetpath-macros-are-empty?forum=msbuild, you have to delete the PropertyGroup in the csproj file, and then to add the build event in Visual Studio again. enter image description here

After you add it again, it will be at the end of the csproj file, and the macros should work.

Workout answered 3/6, 2015 at 6:14 Comment(7)
I found this same issue in VS2015Catholicism
I had the same issue. I had added PropertyGroup it manually in csproj file. I had to delete it and added it again from project properties > Build Events tab and it started working. Thanks for saving my time.Bugbane
I spent so much time troubleshooting this using msbuild on 2017. The critical point here should be that there is a bug? with the .NET SDK that requires the <PostBuildEvent> to come after the <Import> and <Target> elements in the csproj file.Hypoplasia
I manually edited the csproj and moved the <PropertyGroup> section to the very bottom, just above the </Project> tag, and that worked.Sestet
It might be this bug report also: github.com/dotnet/sdk/issues/677Sestet
Or this one github.com/dotnet/project-system/pull/2367 or this one github.com/dotnet/project-system/issues/1569, I can't tell.Sestet
JetBrain's Rider IDE is also affected by this.Vaduz
T
7

Had the same problem when migrating a project file from old .NET framework format to SDK style.

It seems the structure of the PostBuildEvent changed and is not converted by the "upgrade assistant" tool.

This was the command before:

<PropertyGroup>
  <PostBuildEvent>echo ProjectDir: $(ProjectDir)</PostBuildEvent>
</PropertyGroup>

This is the new version:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
  <Exec Command="echo ProjectDir: $(ProjectDir)" />
</Target>

To do the migration: open the code file (right click on project, "Properties"). Then cut the post build snippet and save the file. Paste the snippet again - now it should have the new format.

Tricho answered 22/8, 2022 at 12:32 Comment(0)
H
3

Try $(Configuration) instead of $(ConfigurationMode), I was having this issue too and this solved it, although I don't know why it happens...

Helve answered 4/3, 2015 at 15:24 Comment(0)
R
1

My solution to this was just to make sure that the post build event was the very last thing in the .csproj file.

Remonaremonetize answered 6/1, 2023 at 11:51 Comment(0)
G
0

I think I figured it out. This seems to happen when I have multiple instances of VS open. Try closing all instances or VS and starting up one.

Gaye answered 20/7, 2017 at 18:49 Comment(0)
A
0

Curiously this can still be 'a thing' with VS2022 (though possibly only Preview 17.4.x). I have several .NET Framework projects that can't load their build events unless they are in the old format and can't save them at all.

Attitudinarian answered 13/10, 2022 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.