방법: ClickOnce 배포 API를 사용하여 프로그래밍 방식으로 응용 프로그램 업데이트 확인

업데이트: 2007년 11월

ClickOnce에서는 응용 프로그램이 배포된 후 두 가지 방법으로 업데이트할 수 있습니다. 첫 번째 방법은 ClickOnce 배포를 구성하여 특정 간격에 따라 업데이트를 자동으로 확인하는 것입니다. 두 번째 방법은 ApplicationDeployment 클래스를 사용하는 코드를 작성하여 사용자 요청과 같은 이벤트를 기반으로 업데이트를 확인하는 것입니다.

다음 절차에서는 프로그래밍 방식의 업데이트를 수행하기 위한 일부 코드를 보여 주고 프로그래밍 방식의 업데이트 확인을 사용할 수 있도록 ClickOnce 배포를 구성하는 방법도 설명합니다.

ClickOnce 응용 프로그램을 프로그래밍 방식으로 업데이트하려면 업데이트 위치를 지정해야 합니다. 이를 배포 공급자라고 합니다. 이 속성 설정에 대한 자세한 내용은 ClickOnce 업데이트 전략 선택을 참조하십시오.

참고:

아래에 설명된 방법을 사용하여 응용 프로그램을 배포한 위치와는 다른 위치에서 응용 프로그램을 업데이트할 수도 있습니다. 자세한 내용은 방법: 배포 업데이트를 위한 대체 위치 지정을 참조하십시오.

프로그래밍 방식으로 업데이트를 확인하려면

  1. 기본 명령줄 또는 비주얼 도구를 사용하여 새 Windows Forms 응용 프로그램을 만듭니다.

  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에서 업데이트를 검사할 응용 프로그램 매니페스트로 설정합니다. 업데이트 옵션 탭에서 이 응용 프로그램의 업데이트 확인 확인란의 선택을 취소합니다.

보안

프로그래밍 방식의 업데이트를 사용하려면 응용 프로그램에 완전 신뢰 권한이 있어야 합니다.

참고 항목

작업

방법: 배포 업데이트를 위한 대체 위치 지정

개념

ClickOnce 업데이트 전략 선택

ClickOnce 배포 개요