6

I am using Visual Stduio 2015 Community Edition. I have loaded a C# project and want to configure it to use shadow building, so that source files are not messed up with any binaries or intermediate build files. Tried to set the [output]/[intermediate output] directory paths of the C# project using the VS UI - the only UI-editable output path is the "bin" one and it doesn't accept MSBuild macros. Then I've unloaded the project and manually edited it. Also set the IntermediateOutputPath which is not available in the VS UI when using C# project, but when using C++ one all such kind of directories are editable. Currently, the OutputPath is set to: <OutputPath>$(SolutionDir)..\build_$(SolutionName)_$(Configuration)_$(Platform)\$(ProjectName)</OutputPath>

$(ProjectName) is empty for some reason. Tried $(TargetName) (it is not correct to use it but just for the test) but it is empty, too. Any suggestions?

Ivan Caravanio
  • 597
  • 1
  • 7
  • 20
  • 1
    *[Don't use `$(ProjectDir)`, but instead use `$(MSBuildProjectDir`)](https://stackoverflow.com/questions/2647373/when-does-msbuild-set-the-projectname-property/2652555#2652555)*. – Peter Mortensen Jul 25 '19 at 23:41

2 Answers2

3

Do not try to import Microsoft.CSharp.targets - like suggested in another answer. First of all - it will lead to additional warning like

Warning MSB4011: "Microsoft.CSharp.targets" cannot be imported again. ...

But additionally to that if you try to edit pre-/post- build Visual studio will crash. (Observed in Visual studio 2019, 16.7.7 version)

Better to use $(MSBuildProjectName) macro instead of $(ProjectName) - works identically.

TarmoPikaro
  • 4,723
  • 2
  • 50
  • 62
2

The problem is that some of the MSBuild properties like ProjectName are set after the following line in your .csproj file.

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

After this line you can access all variables.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steven Spyrka
  • 678
  • 7
  • 18
  • In turn, this file depends on other properties, such as `OutputPath`, to be set. These are set by a number of `PropertyGroup` tags in the project file, one for each project configuration (configuration|platform). So these must come first, followed by the import, followed by the `PropertyGroup` which defines the post-build event. – Florian Winter Sep 05 '17 at 10:05
  • 2
    Yes, and *[don't use `$(ProjectDir)`, but instead use `$(MSBuildProjectDir`)](https://stackoverflow.com/questions/2647373/when-does-msbuild-set-the-projectname-property/2652555#2652555)*. – Peter Mortensen Jul 25 '19 at 23:40
  • 1
    @PeterMortensen: your comment should be the answer. – g.pickardou May 01 '20 at 07:10
  • Agreed. @PeterMortensen please consider promoting your comments to an actual answer here. – julealgon Dec 16 '20 at 20:38
  • Error MSB3644 The reference assemblies for .NETFramework,Version=v4.0 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks – Andrei Krasutski Mar 29 '23 at 16:54