共用方式為


HOW TO:使用 ClickOnce 部署 API 以程式設計方式檢查應用程式更新

更新:2007 年 11 月

ClickOnce 提供可在部署應用程式時進行更新的兩種方式。若使用第一種方法,您可以設定 ClickOnce 部署在某些間隔時間自動檢查更新;若使用第二種方法,您可以撰寫使用 ApplicationDeployment 類別的程式碼,以便根據事件 (如使用者要求) 檢查更新。

下列程序會示範某些用來執行程式設計方式更新的程式碼,同時也說明如何設定 ClickOnce 部署,使其以程式設計方式檢查更新。

如果要以程式設計的方式更新 ClickOnce 應用程式,必須指定更新的位置。有時稱為部署提供者。如需設定這項屬性的詳細資訊,請參閱選擇 ClickOnce 更新策略

注意事項:

您也可以使用如下所述的技巧,從某個位置部署您的應用程式,但是從另一個位置進行更新。如需詳細資訊,請參閱 HOW TO:指定部署更新的其他位置

若要以程式設計方式檢查更新

  1. 使用偏好的命令列或視覺化工具建立新的 Windows Form 應用程式。

  2. 建立按鈕、功能表項目或其他使用者介面項目,讓使用者選取該項目即可檢查更新。從該項目的事件處理常式中,呼叫下列方法,以檢查及安裝更新。

    Private Sub InstallUpdateSyncWithInfo()
        Dim info As UpdateCheckInfo = Nothing
    
        If (ApplicationDeployment.IsNetworkDeployed) Then
            Dim AD As ApplicationDeployment = ApplicationDeployment.CurrentDeployment
    
            Try
                info = AD.CheckForDetailedUpdate()
            Catch dde As DeploymentDownloadException
                MessageBox.Show("The new version of the application cannot be downloaded at this time. " + ControlChars.Lf & ControlChars.Lf & "Please check your network connection, or try again later. Error: " + dde.Message)
                Return
            Catch ioe As InvalidOperationException
                MessageBox.Show("This application cannot be updated. It is likely not a ClickOnce application. Error: " & ioe.Message)
                Return
            End Try
    
            If (info.UpdateAvailable) Then
                Dim doUpdate As Boolean = True
    
                If (Not info.IsUpdateRequired) Then
                    Dim dr As DialogResult = MessageBox.Show("An update is available. Would you like to update the application now?", "Update Available", MessageBoxButtons.OKCancel)
                    If (Not System.Windows.Forms.DialogResult.OK = dr) Then
                        doUpdate = False
                    End If
                Else
                    ' Display a message that the app MUST reboot. Display the minimum required version.
                    MessageBox.Show("This application has detected a mandatory update from your current " & _
                        "version to version " & info.MinimumRequiredVersion.ToString() & _
                        ". The application will now install the update and restart.", _
                        "Update Available", MessageBoxButtons.OK, _
                        MessageBoxIcon.Information)
                End If
    
                If (doUpdate) Then
                    Try
                        AD.Update()
                        MessageBox.Show("The application has been upgraded, and will now restart.")
                        Application.Restart()
                    Catch dde As DeploymentDownloadException
                        MessageBox.Show("Cannot install the latest version of the application. " & ControlChars.Lf & ControlChars.Lf & "Please check your network connection, or try again later.")
                        Return
                    End Try
                End If
            End If
            End If
    End Sub
    
    private void InstallUpdateSyncWithInfo()
    {
        UpdateCheckInfo info = null;
    
        if (ApplicationDeployment.IsNetworkDeployed)
        {
            ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
    
            try
            {
                info = ad.CheckForDetailedUpdate();
    
            }
            catch (DeploymentDownloadException dde)
            {
                MessageBox.Show("The new version of the application cannot be downloaded at this time. \n\nPlease check your network connection, or try again later. Error: " + dde.Message);
                return;
            }
            catch (InvalidDeploymentException ide)
            {
                MessageBox.Show("Cannot check for a new version of the application. The ClickOnce deployment is corrupt. Please redeploy the application and try again. Error: " + ide.Message);
                return;
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show("This application cannot be updated. It is likely not a ClickOnce application. Error: " + ioe.Message);
                return;
            }
    
            if (info.UpdateAvailable)
            {
                Boolean doUpdate = true;
    
                if (!info.IsUpdateRequired)
                {
                    DialogResult dr = MessageBox.Show("An update is available. Would you like to update the application now?", "Update Available", MessageBoxButtons.OKCancel);
                    if (!(DialogResult.OK == dr))
                    {
                        doUpdate = false;
                    }
                }
                else
                {
                    // Display a message that the app MUST reboot. Display the minimum required version.
                    MessageBox.Show("This application has detected a mandatory update from your current " + 
                        "version to version " + info.MinimumRequiredVersion.ToString() + 
                        ". The application will now install the update and restart.", 
                        "Update Available", MessageBoxButtons.OK, 
                        MessageBoxIcon.Information);
                }
    
                if (doUpdate)
                {
                    try
                    {
                        ad.Update();
                        MessageBox.Show("The application has been upgraded, and will now restart.");
                        Application.Restart();
                    }
                    catch (DeploymentDownloadException dde)
                    {
                        MessageBox.Show("Cannot install the latest version of the application. \n\nPlease check your network connection, or try again later. Error: " + dde);
                        return;
                    }
                }
            }
        }
    }
    
    public:
        void InstallUpdateSync()
        {
            if (ApplicationDeployment::IsNetworkDeployed)
            {
                bool isUpdateAvailable = false;
                ApplicationDeployment^ appDeployment =
                    ApplicationDeployment::CurrentDeployment;
    
                try
                {
                    isUpdateAvailable = appDeployment->CheckForUpdate();
                }
                catch (InvalidOperationException^ ex)
                {
                    MessageBox::Show("The update check failed. Error: {0}",
                        ex->Message);
                    return;
                }
    
                if (isUpdateAvailable)
                {
                    try
                    {
                        appDeployment->Update();
                        MessageBox::Show(
                            "The application has been upgraded, and will now " +
                            "restart.");
                        Application::Restart();
                    }
                    catch (Exception^ ex)
                    {
                        MessageBox::Show("The update failed. Error: {0}",
                            ex->Message);
                        return;
                    }
    
                }
            }
        }
    
  3. 編譯應用程式。

使用 Mage.exe 部署以程式設計方式檢查更新的應用程式

  • 請遵循逐步解說:手動部署 ClickOnce 應用程式中說明的指示,使用 Mage.exe 部署您的應用程式。在呼叫 Mage.exe 產生部署資訊清單時,請務必使用命令列參數 providerUrl,並指定 ClickOnce 應前往檢查更新的 URL。例如,如果您的應用程式會從 http://www.adatum.com/MyApp 進行更新,則用來產生部署資訊清單的呼叫可能如下所示:

    mage -New Deployment -ToFile WindowsFormsApp1.application -Name "My App 1.0" -Version 1.0.0.0 -AppManifest 1.0.0.0\MyApp.manifest -providerUrl http://www.adatum.com/MyApp/MyApp.application
    

使用 MageUI.exe 來部署以程式設計方式檢查更新的應用程式

  • 請遵循逐步解說:手動部署 ClickOnce 應用程式中說明的指示,使用 Mage.exe 部署您的應用程式。在 [部署選項] 索引標籤上,將 [開始位置] 欄位設定為 ClickOnce 應檢查更新的應用程式資訊清單。在 [更新選項] 索引標籤上,清除 [此應用程式應該檢查更新檔] 核取方塊。

安全性

您的應用程式必須具有完全信任的權限,才能使用程式設計方式的更新。

請參閱

工作

HOW TO:指定部署更新的其他位置

概念

選擇 ClickOnce 更新策略

ClickOnce 部署概觀