Visual Studio: add After-Publish script

Visual Studio: add After-Publish script

For a project, I needed to execute a script to patch a file after it has been published to a local folder.
After-Publish script aren't editable in the Visual Studio editor (contrary to Pre-Build and Post-Build), but they do exist, even if not well documented (and note the non-coherent naming convention...).

After some researches, I have found that you can manually edit them in your project file.

Here is an example:

	<Target Name="PostBuild" AfterTargets="PostBuildEvent">
		<Exec Command="echo BEGIN Post-build&#xD;&#xA;&#xD;&#xA;echo END Post-build" />
	</Target>
	<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
		<Exec Command="echo BEGIN Pre-build&#xD;&#xA;&#xD;&#xA;echo END Pre-build" />
	</Target>
	<Target Name="PostPublish" AfterTargets="AfterPublish">
		<Exec Command="echo BEGIN After-publish&#xD;&#xA;&#xD;&#xA;PublishDir=$(PublishDir)&#xD;&#xA;PublishUrl=$(PublishUrl)&#xD;&#xA;&#xD;&#xA;echo END After-publish" />
	</Target>

Note that you can use both PublishDir and PublishUrl, but they have different meanings:

  • PublishDir is the absolute path to the temporary folder used for publication, something like $(ProjectDir)\obj\Release\netstandardapp2.1\PubTmp\Out\
  • PublishUrl is the relative path to the target folder, something like bin\Release\netstandard2.1\publish\