Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The way MSBuild works when processing files is to read all files and create one in memory representation of all those files. This all happens before any target is executed. Because of this when a target is executing it has no concept of what file it was contained in. Basically you will not be able to use relative paths inside of .targets files. The way to deal with this situation is to ensure that your outlying .proj files (<em>or whatever extension you use</em>) to declare a known property which your .targets file uses to resolve the full path to the shared files.</p> <p>For instance</p> <p><strong>Sample.targets</strong></p> <pre><code>&lt;Project ...&gt; &lt;Target Name="ExecTool"&gt; &lt;Exec Command="$(YourToolPath)tool.exe" /&gt; &lt;/Target&gt; &lt;/Project&gt; </code></pre> <p><strong>Build.proj</strong></p> <pre><code>&lt;Project ...&gt; &lt;PropertyGroup&gt; &lt;YourToolPath&gt;$(MSBuildProjectDirectory)\..\&lt;/YourToolPath&gt; &lt;/PropertyGroup&gt; &lt;Import Project="..\..\..\Sample.targets"/&gt; &lt;/Project&gt; </code></pre> <p>In a related note, I discussed validating such "shared" properties on my blog a while back at <a href="http://sedodream.com/2009/06/30/ElementsOfReusableMSBuildScriptsValidation.aspx" rel="nofollow noreferrer">Elements of Reusable MSBuild Scripts: Validation</a>.</p> <h2>MSBuild v. 4.0 and above</h2> <p>If you are using <strong>MSBuild 4.0</strong> (or above), i.e. Visual Studio 2010/.NET 4.0, (which can target .NET 2.0/3.0/3.5) as well. Then you now have <a href="https://msdn.microsoft.com/en-us/library/ms164309.aspx" rel="nofollow noreferrer">these properties</a> which can be used for this specific purpose:</p> <ul> <li><code>MSBuildThisFile</code></li> <li><code>MSBuildThisFileDirectory</code></li> <li><code>MSBuildThisFileDirectoryNoRoot</code></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload