Hugh W
Hugh W

Reputation: 1216

How to reproduce Visual Studio ClickOnce publish from command line

The Publish button in Visual Studio 2022

I have been publishing my app by clicking the Publish button in the VS2022 project Publish tool. I want to recreate the same behaviour from the command line.

My C# project targets .NET 6.0, and my .pubxml has <PublishProtocol>ClickOnce</PublishProtocol>.

These docs sound like they should document what I need, but the suggested variations on msbuild /target:publish absolutely do not produce the same result as clicking the button in VS:

Upvotes: 5

Views: 907

Answers (1)

Bowman Zhu
Bowman Zhu

Reputation: 7146

Update:

A script to achieve your requirements:

#find the .csproj file
$csproj = Get-ChildItem -Path . -Filter *.csproj

# #return the name of the project
$projectname = $csproj.Name -replace ".csproj",""

$publishdir = "bin\publish"
$publishXML = "Properties\PublishProfiles\ClickOnceProfile.pubxml"

#remove all content of the "$publishdir/Application Files" directory
Get-ChildItem -Path $publishdir"\Application Files" | Remove-Item -Recurse

MSBuild.exe /target:publish /p:COnfiguration=publish /p:PublishProfile=Properties\PublishProfiles\ClickOnceProfile.pubxml /p:PublishDir=$publishdir

#remove all files or directories except setup.exe, projectname.application and the directory "Application Files"
Get-ChildItem -Path $publishdir | Where-Object {$_.Name -ne "setup.exe" -and $_.Name -ne $projectname+".application" -and $_.Name -ne "Application Files"} | Remove-Item -Recurse

#get the content of Project.PropertyGroup.ApplicationRevision in the .pubxml file
$pubxmldata = [xml](Get-Content $publishXML)
$ApplicationRevision = $pubxmldata.Project.PropertyGroup.ApplicationRevision

#increment the ApplicationRevision value
$ApplicationRevision = [int]$ApplicationRevision + 1

#replace the ApplicationRevision value in the .pubxml file
$pubxmldata.Project.PropertyGroup.ApplicationRevision = [string]$ApplicationRevision

#save the .pubxml file
$pubxmldata.Save($publishXML)

Original Answer:

The <PublishDir> and <PublishUrl> from the .pubxml are ignored.

The <ApplicationRevision> is not updated even when is true.

Most importantly, the output is missing the main MyApp.application file and the Application Files folder.

For the first and the third requirement, using the below command will solve the issue:

msbuild -t:restore 

msbuild /target:publish /p:Configuration=publish /p:PublishProfile=Properties\PublishProfiles\ClickOnceProfile.pubxml /p:PublishDir=bin\Release\net6.0-windows\app.publish\ /p:PublishUrl="bin\publish\"

Result:

enter image description here

enter image description here

For the second requirement, the value didn't increase is totally expected, because it is not automatically incremented for builds performed at the command-line. See this official document:

Publish Properties

enter image description here

Increase this value need based on IDE, pure msbuild command can't achieve this.

Upvotes: 3

Related Questions