Can $(SolutionDir) variable be used in a Visual Studio publish profile?

1000 Views Asked by At

Using Visual Studio 2017, in the solution explorer, I right-click on a C# .Net Core project and select "Publish...". I create a publish profile that publishes the app to a folder or file share. The result is a profile whose default target location is an absolute path "[project directory]\bin\Release\netcoreapp2.1\publish\", such as in the following snippet.

Visual Studio 2017 Publish Profile Settings dialog

Visual Studio generates a .pubxml file to store that publish profile, where the target location is stored in a <PublishDir> tag. For example:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <PublishProtocol>FileSystem</PublishProtocol>
    <Configuration>Release</Configuration>
    <Platform>Any CPU</Platform>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <PublishDir>F:\work\foo\bin\Release\netcoreapp2.1\publish\</PublishDir>
  </PropertyGroup>
</Project>

What I would prefer is to use a variable in the value of the <PublishDir> tag, such as the $(SolutionDir) variable. I can manually edit the .pubxml file to inject a variable. For example:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    ...
    <PublishDir>$(SolutionDir)\publish\</PublishDir>
  </PropertyGroup>
</Project>

However, when I next load the project in Visual Studio, that variable is either ignored, or has a empty value, such as in the following snippet.

enter image description here

Is there a way to use variables in the publish profile?

1

There are 1 best solutions below

0
newbie On

Had a similar problem using VS2019. Only things that worked for me where environment variables like $(windir) and Properties in the pubxml above the line where they are needed e.g.

<!-- removed some properties from xml -->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <PublishProvider>FileSystem</PublishProvider>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <PublishUrl>$(WINDIR)\$(PublishProvider)\$(WebPublishMethod)\$(LastUsedBuildConfiguration)_$(TargetFramework)</PublishUrl>
    <WebPublishMethod>FileSystem</WebPublishMethod>
  </PropertyGroup>
</Project>

The resulting PublishUrl was

C:\Windows\FileSystem\\Release_netcoreapp3.1

WebPublishMethod is not part of PublishUrl because it's below PublishUrl.

Don't know about F:\work\foo\bin\ but you could try Configuration and TargetFramework for Release\netcoreapp2.1