Automatically install updates with ClickOnce deplo

2020-08-17 18:23发布

问题:

I wanted to deploy my project with ClickOnce deployment. But when I did it like that, it was asking in a dialog box at the end user machine:

A new version of XXXX is available. Do you want to download it now?

But my end users don't have a mouse or keyboard. So my intention is: It must take the updates automatically, but it must NOT ask that dialog box at the client side. How do I achieve this by using ClickOnce deployment?

回答1:

Looks like you can do this by changing some properties in the build.

http://blog.jnericks.com/configuring-msbuild-to-auto-update-clickonce

  1. MinimumRequiredVersion - Tells ClickOnce that when it updates this application it should update to this version (however this does not force ClickOnce to perform the update). As you can see we set this to the same version number that the ApplicationVersion is set to so that the MinimumRequiredVersion is always the latest version.
  2. UpdateMode=Foreground - Tells ClickOnce to update the application before it is opened.
  3. UpdateRequired=True - Tells ClickOnce to automatically perform the update.

No MSBuild scenario:

  1. Right Click your project and select Properties
  2. Go to the "Publish" tab on the bottom left
  3. Click the "Updates..." button to open the Application Updates dialog
  4. Check "The application should check for updates"
  5. Select "Before the application starts"
  6. Check "Specify a minimum required version for this application"
  7. Enter the Publish Version that you can see in the underlying Publish window as the minimum version. Unfortunately, you have to change this every publish. There might be a way for this to be auto, though.

Then publish the application and test it. This was worked fine for me on a local test application.

Edit: looks like some people have been getting the minimum required version to update, might want to look into their solutions.

Edit 2: Image showing where versioning is important:

Also, note I have "Automatically increment revision with each publish" checked. Every time you go into the Properties for the project, that version will be up to date. You'll generally just have to change the "Revision" part of the Version in the "Application Updates" window to match the "Revision" in the Publish tab.



回答2:

Sure can! As long as it's a network-deployed application, you can easily check for updates using this code. See below:

Private Sub InstallUpdates()
    Dim info As UpdateCheckInfo = Nothing
    If (ApplicationDeployment.IsNetworkDeployed) Then
        Dim AD As ApplicationDeployment = ApplicationDeployment.CurrentDeployment
        Try
            info = AD.CheckForDetailedUpdate()
        Catch dde As DeploymentDownloadException
            (You may want to log here)
            Return
        Catch ioe As InvalidOperationException
            (You may want to log here)
            Return
        End Try
        If (info.UpdateAvailable) Then
            Try
                AD.Update()
                Application.Restart()
            Catch dde As DeploymentDownloadException
                (You may want to log here)
                Return
            End Try
        End If
    End If
End Sub

You can enter this snippet and call it in the startup. It works in console applications, Windows Forms applications, but only if you are network deployed! Where you see all my comments about logging is where I was originally using message boxes with prompts, but this is the version that doesn't require any input!



回答3:

In addition to Gromer's answer, simply install the AutoUpdateProjectsMinimumRequiredClickOnceVersion nuget package in your project. Once you have your project set to check for updates and to use a minimum required version, this will handle making sure the minimum required version always matches your current version (i.e. user's will always be forced to update to the latest version).



回答4:

Any ClickOnce application based on an .exe file can be silently installed and updated by a custom installer. A custom installer can implement custom user experience during installation, including custom dialog boxes for security and maintenance operations. To perform installation operations, the custom installer uses the InPlaceHostingManager class.

For implementing this solution please refer to this link



回答5:

I know it's an old Q. but, I will answer anyway. (hope it will help someone):

First, you need to check: Choose when the application should check for updates >> After the application starts.

Secondly, add this method to your code:

private Boolean isVersionOK()
{
    UpdateCheckInfo info = null;

    if (ApplicationDeployment.IsNetworkDeployed)
    {
        ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;

        try
        {
            info = ad.CheckForDetailedUpdate();
        }
        catch (DeploymentDownloadException)
        {
            // No network connection
            return false;
        }
        catch (InvalidDeploymentException)
        {
            return false;
        }
        catch (InvalidOperationException)
        {
            return false;
        }

        if (info.UpdateAvailable)
        {
            try
            {
                ad.Update();
                Application.Restart();
                Environment.Exit(0);
            }
            catch (DeploymentDownloadException)
            {
                // No network connection
            }

            return false;
        }
        return true;
    }
    else
    {
        return false;
    }
}

Lastly, you just need to call isVersionOK() at the start of your app and in every few loops as needed to check for update. it will return TRUE if you are on your latest version otherwise it will return FALSE and expect the app will restart to a newer version automatically without user interaction.



回答6:

In follow-up of Ahmed's answer, below is the code in VB.NET with minor enhancements. It might not be as per the best practices yet it is readable and descriptive.

''' <summary>
''' Checks if the update is available for network based deployment and download it.
''' </summary>
''' <param name="autoDownloadUpdate">If the update is available, should it be downloaded automatically.<para>Default value is <code>True</code></para></param>
''' <returns>It will return <code>True</code> only if the latest version is already installed.
''' <para>If autoDownloadUpdate is set to <code>True</code>, the update is auto downloaded (and app restarts and nothing is returned) else it returns <code>False</code>.</para>
''' </returns>
Shared Private Function CheckAndDownloadUpdate(ByVal Optional autoDownloadUpdate As Boolean = True) As Boolean

    If ApplicationDeployment.IsNetworkDeployed = False Then Return False

    Dim appDeployment As ApplicationDeployment = ApplicationDeployment.CurrentDeployment
    Dim info As UpdateCheckInfo = Nothing

    Try
        info = appDeployment.CheckForDetailedUpdate
    Catch ex As Exception
        ' Exceptions if you want to handle individually
        'DeploymentDownloadException ' No network connection
        'InvalidDeploymentException
        'InvalidOperationException
        Return False
    End Try

    ' If no update is available, it means latest version is installated
    If info.UpdateAvailable = False Then Return True

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    ' If we are here, it means an update is available on the network

    ' if autoDownload is False, simply return False 
    If autoDownloadUpdate = False Then Return False

    Try
        appDeployment.Update()
        Application.Restart()
        Environment.Exit(0)
    Catch ex As DeploymentDownloadException
        ' No network connection
        Return False
    End Try

End Function

An then in your startup code, you can call like this

CheckAndDownloadUpdate()

Any feedback to further ehance the answer...