Automatically force your ClickOnce app to update itself without prompting the user; this is less obtrusive to the user experience when receiving updates, and enhances security by ensuring the latest version is always used.
This is a PowerShell script that updates the ClickOnce application's minimum required version to the latest published version. This will eliminate the prompt that asks the user if they want to download and install the latest version; instead the update will automatically be downloaded and installed.
The installation instructions below are intended to be used when publishing your ClickOnce app from Visual Studio. If you are looking to publish your ClickOnce app from a CI/CD pipeline, check out this blog post.
If you are using a .NET Framework project, as well as the packages.config
package management format, simply install the AutoUpdateProjectsMinimumRequiredClickOnceVersion NuGet package to your project, and it will automatically handle all of the installation for you.
NOTE: If you are using the PackageReference NuGet package management format, you will need to follow the manual installation instructions in the following section below.
As you can see in the last screenshot above, the NuGet package will add a PostBuildScripts
folder to your project that contains the AutoUpdateProjectsMinimumRequiredClickOnceVersion.ps1 PowerShell script that is ran after each build.
It also update's the project file to add a post-build event to run the PowerShell script.
.NET Core projects use the PackageReference NuGet package management format, where NuGet package references are stored directly in the project file instead of a separate packages.config
file.
Unfortunately the PackageReference format does not support NuGet packages running scripts during installation, so it cannot automatically add the required post-build event to the project.
Instead of using the NuGet package, you will instead need to manually add the PowerShell script to your project, and add the post-build event to the project file. The steps to do this are:
-
In Visual Studio, right-click on your project and add a new folder called
PostBuildScripts
. -
Download the latest AutoUpdateProjectsMinimumRequiredClickOnceVersion.ps1 PowerShell script and save it to the
PostBuildScripts
folder. -
You should now see the PowerShell script in the
PostBuildScripts
folder in Solution Explorer.- If you do not see the PowerShell file in Visual Studio, right-click on the
PostBuildScripts
folder and chooseAdd
>Existing Item...
. Select the PowerShell script that you downloaded and saved to thePostBuildScripts
folder.
- If you do not see the PowerShell file in Visual Studio, right-click on the
-
In Visual Studio, right-click on the project and choose
Properties
. -
Navigate to the
Build
>Events
tab, and paste the following code into thePost-build event
text box:REM Update the ClickOnce MinimumRequiredVersion so that it auto-updates without prompting. PowerShell -ExecutionPolicy Bypass -Command "& '$(ProjectDir)PostBuildScripts\AutoUpdateProjectsMinimumRequiredClickOnceVersion.ps1' -ProjectFilePaths '$(ProjectPath)'"
The end result should look like this:
If you have multiple projects that you deploy via ClickOnce, you'll want to do the steps above for each project.
After the installation, if you haven't published your ClickOnce app yet, when you build your project it may fail with an error like:
MSB3073
PowerShell script exited with code 1.
If you check the Output
pane in Visual Studio, it may mention that your project does not have any ClickOnce deployment settings in it.
Before the build will succeed, you need to configure those settings.
.NET Framework projects store their ClickOnce settings in the project file. To configure the settings:
-
You can access the project settings by right-clicking the project in Visual Studio and choosing
Properties
. -
In the project properties, on the
Publish
tab, in theUpdates...
button, make sure the following options are checked:- The application should check for updates
- Specify a minimum required version for this application
.NET Core projects store their ClickOnce settings in a publish profile xml file. To configure the settings:
-
You can edit the publish profile settings by right-clicking the project in Visual Studio and choosing
Publish...
to get to the wizard. -
If you have multiple publish profiles, choose the appropriate ClickOnce profile, or create a new one.
-
In the Publish wizard, within the
Settings
tab, make sure the following options are checked:- The application should check for updates
- In the
Update Settings
window, Specify a minimum required version for this application
If for some reason the script is not updating your project's MinimumRequiredVersion to the latest published version, check the Visual Studio Output
window for error messages thrown by the PowerShell script.
Detailed help documentation for manually running the PowerShell script can be obtained by running the Get-Help cmdlet against the script in a PowerShell window.
For example, open up a Windows PowerShell command prompt, navigate to the folder containing the AutoUpdateProjectsMinimumRequiredClickOnceVersion.ps1 script, and enter:
Get-Help .\AutoUpdateProjectsMinimumRequiredClickOnceVersion.ps1 -Detailed
The first build after each ClickOnce deployment will update the .csproj file, and the project will need to be reloaded. If you are running an older version of Visual Studio (2012 or earlier), to prevent the reloading of the project from closing any tabs that you have open I recommend installing the Workspace Reloader Visual Studio extension.
Buy me a hot chocolate for providing this project open source and for free 🙂