21

If I have a project structure like this:

\MySolution
  \MyProject
    ReadMe.md
    \build
      MyProject.targets

What would the value of $(MSBuildThisFileDirectory) be when used in the MyProject.targets file?

Assuming my solution folder is in the root of C: drive, would it be?..

c:\MySolution\MyProject\build\

In the MyProject.targets file, how would I reference the ReadMe.md file using the $(MSBuildThisFileDirectory)?

Additional information:

MyProject.targets looks like:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <None Include="$(MSBuildThisFileDirectory)\xxx\ReadMe.md">
      <Link>FrameworkTests.feature</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CustomToolNamespace></CustomToolNamespace>
    </None>
  </ItemGroup>
</Project>
riQQ
  • 9,878
  • 7
  • 49
  • 66
Matt W
  • 11,753
  • 25
  • 118
  • 215

1 Answers1

11

What is the value of MSBuildThisFileDirectory?

It depends on your MyProject.targets. According to the literal meaning of this variable, you could to know ThisFileDirectory means "This File Directory".

Since you have use this argument in the file MyProject.targets, the path should be related to the location of the "this file" MyProject.targets. So the value of this argument should be the directory of this file MyProject.targets.

After install the nuget, the file MyProject.targets should be added to the path:

c:\MySolution\packages\MyProject.1.0.0<YouPackagefolder>\build

You can use a target to output that value in your project file, to accomplish this, unload your project. Then at the very end of the project, just before the end-tag </project>, place below scripts:

<Target Name="TestValue" AfterTargets="build">
  <Message Text="@(none)">
  </Message>
</Target>
Petter Hesselberg
  • 5,062
  • 2
  • 24
  • 42
Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • 1
    So, given that the `ReadMe.md` file gets copied into the project (which my NuGet package is being installed into) how would I reference it in the .targets file (with a view to doing something to it)? – Matt W Apr 12 '18 at 11:26
  • 1
    @MattW, do you mean you want reference the `ReadMe.md` file using the $(MSBuildThisFileDirectory) in the MyProject.targets file? If yes, I am afraid you could not do such thing with $(MSBuildThisFileDirectory), just like what I said in the previous question, since the `ReadMe.md` file located in the parent folder of `\build`, if you sue $(MSBuildThisFileDirectory), you should use `$(MSBuildThisFileDirectory)..\`, which could not be parse by MSBuild, you can use $(ProjectDir)ReadMe.md in the `.targets`. – Leo Liu Apr 12 '18 at 13:52
  • And you can't change the targets file itself. It's considered static and part of the nuget. If you want it copied to the project, you need to put an additional targets file in the package and copy it to the project if it's not present. Then use the targets file in the `builld` folder to automatically include the targets file in the project folder if it exists. – jessehouwing May 30 '19 at 09:09