다음을 통해 공유


연습: ClickOnce 애플리케이션에 대한 사용자 지정 설치 관리자 만들기

.exe 파일을 기반으로 하는 모든 ClickOnce 애플리케이션은 사용자 지정 설치 관리자로 자동으로 설치하고 업데이트할 수 있습니다. 사용자 지정 설치 관리자는 설치 중에 보안 및 유지 관리 작업을 위한 사용자 지정 대화 상자를 포함하여 사용자 지정 사용자 환경을 구현할 수 있습니다. 설치 작업을 수행하기 위해 사용자 지정 설치 관리자는 InPlaceHostingManager 클래스를 사용합니다. 이 연습에서는 ClickOnce 애플리케이션을 자동으로 설치하는 사용자 지정 설치 관리자를 만드는 방법을 보여줍니다.

참고

System.Deployment.Application 네임스페이스의 ApplicationDeployment 클래스 및 API는 .NET Core 및 .NET 5 이상 버전에서 지원되지 않습니다. .NET 7에서 애플리케이션 배포 속성에 액세스하는 새로운 방법을 지원합니다. 자세한 내용은 .NET에서 ClickOnce 배포 속성 액세스를 참조하세요. .NET 7은 ApplicationDeployment 메서드와 동등한 메서드를 지원하지 않습니다.

필수 조건

사용자 지정 ClickOnce 애플리케이션 설치 관리자를 만들려면

  1. ClickOnce 애플리케이션에서 System.Deployment 및 System.Windows.Forms에 대한 참조를 추가합니다.

  2. 애플리케이션에 새 클래스를 추가하고 이름을 지정합니다. 이 연습에서는 MyInstaller라는 이름을 사용합니다.

  3. 다음 Imports 또는 using 지시문을 새 클래스의 맨 위에 추가합니다.

    using System.Deployment.Application;
    using System.Windows.Forms;
    
  4. 클래스에 다음 메서드를 추가합니다.

    이러한 메서드는 InPlaceHostingManager 메서드를 호출하여 배포 매니페스트를 다운로드하고, 적절한 권한을 어설션하고, 사용자에게 설치 권한을 요청한 다음, 애플리케이션을 다운로드하여 ClickOnce 캐시에 설치합니다. 사용자 지정 설치 관리자가 ClickOnce 애플리케이션이 미리 신뢰할 수 있도록 지정하거나 AssertApplicationRequirements 메서드 호출에 대한 신뢰 결정을 연기할 수 있습니다. 이 코드는 애플리케이션을 미리 신뢰합니다.

    참고

    미리 신뢰로 할당된 권한은 사용자 지정 설치 관리자 코드의 사용 권한을 초과할 수 없습니다.

    InPlaceHostingManager iphm = null;
    
    public void InstallApplication(string deployManifestUriStr)
    {
        try
        {
            Uri deploymentUri = new Uri(deployManifestUriStr);
            iphm = new InPlaceHostingManager(deploymentUri, false);
        }
        catch (UriFormatException uriEx)
        {
            MessageBox.Show("Cannot install the application: " + 
                "The deployment manifest URL supplied is not a valid URL. " +
                "Error: " + uriEx.Message);
            return;
        }
        catch (PlatformNotSupportedException platformEx)
        {
            MessageBox.Show("Cannot install the application: " + 
                "This program requires Windows XP or higher. " +
                "Error: " + platformEx.Message);
            return;
        }
        catch (ArgumentException argumentEx)
        {
            MessageBox.Show("Cannot install the application: " + 
                "The deployment manifest URL supplied is not a valid URL. " +
                "Error: " + argumentEx.Message);
            return;
        }
    
        iphm.GetManifestCompleted += new EventHandler<GetManifestCompletedEventArgs>(iphm_GetManifestCompleted);
        iphm.GetManifestAsync();
    }
    
    void iphm_GetManifestCompleted(object sender, GetManifestCompletedEventArgs e)
    {
        // Check for an error.
        if (e.Error != null)
        {
            // Cancel download and install.
            MessageBox.Show("Could not download manifest. Error: " + e.Error.Message);
            return;
        }
    
        // bool isFullTrust = CheckForFullTrust(e.ApplicationManifest);
    
        // Verify this application can be installed.
        try
        {
            // the true parameter allows InPlaceHostingManager
            // to grant the permissions requested in the applicaiton manifest.
            iphm.AssertApplicationRequirements(true) ; 
        }
        catch (Exception ex)
        {
            MessageBox.Show("An error occurred while verifying the application. " +
                "Error: " + ex.Message);
            return;
        }
    
        // Use the information from GetManifestCompleted() to confirm 
        // that the user wants to proceed.
        string appInfo = "Application Name: " + e.ProductName;
        appInfo += "\nVersion: " + e.Version;
        appInfo += "\nSupport/Help Requests: " + (e.SupportUri != null ?
            e.SupportUri.ToString() : "N/A");
        appInfo += "\n\nConfirmed that this application can run with its requested permissions.";
        // if (isFullTrust)
        // appInfo += "\n\nThis application requires full trust in order to run.";
        appInfo += "\n\nProceed with installation?";
    
        DialogResult dr = MessageBox.Show(appInfo, "Confirm Application Install",
            MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
        if (dr != System.Windows.Forms.DialogResult.OK)
        {
            return;
        }
    
        // Download the deployment manifest. 
        iphm.DownloadProgressChanged += new EventHandler<DownloadProgressChangedEventArgs>(iphm_DownloadProgressChanged);
        iphm.DownloadApplicationCompleted += new EventHandler<DownloadApplicationCompletedEventArgs>(iphm_DownloadApplicationCompleted);
    
        try
        {
            // Usually this shouldn't throw an exception unless AssertApplicationRequirements() failed, 
            // or you did not call that method before calling this one.
            iphm.DownloadApplicationAsync();
        }
        catch (Exception downloadEx)
        {
            MessageBox.Show("Cannot initiate download of application. Error: " +
                downloadEx.Message);
            return;
        }
    }
    
    /*
    private bool CheckForFullTrust(XmlReader appManifest)
    {
        if (appManifest == null)
        {
            throw (new ArgumentNullException("appManifest cannot be null."));
        }
    
        XAttribute xaUnrestricted =
            XDocument.Load(appManifest)
                .Element("{urn:schemas-microsoft-com:asm.v1}assembly")
                .Element("{urn:schemas-microsoft-com:asm.v2}trustInfo")
                .Element("{urn:schemas-microsoft-com:asm.v2}security")
                .Element("{urn:schemas-microsoft-com:asm.v2}applicationRequestMinimum")
                .Element("{urn:schemas-microsoft-com:asm.v2}PermissionSet")
                .Attribute("Unrestricted"); // Attributes never have a namespace
    
        if (xaUnrestricted != null)
            if (xaUnrestricted.Value == "true")
                return true;
    
        return false;
    }
    */
    
    void iphm_DownloadApplicationCompleted(object sender, DownloadApplicationCompletedEventArgs e)
    {
        // Check for an error.
        if (e.Error != null)
        {
            // Cancel download and install.
            MessageBox.Show("Could not download and install application. Error: " + e.Error.Message);
            return;
        }
    
        // Inform the user that their application is ready for use. 
        MessageBox.Show("Application installed! You may now run it from the Start menu.");
    }
    
    void iphm_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        // you can show percentage of task completed using e.ProgressPercentage
    }
    
  5. 코드에서 설치를 시도하려면 InstallApplication 메서드를 호출합니다. 예를 들어 클래스의 이름을 MyInstaller로 지정한 경우 다음과 같은 방법으로 InstallApplication를 호출할 수 있습니다.

    MyInstaller installer = new MyInstaller();
    installer.InstallApplication(@"\\myServer\myShare\myApp.application");
    MessageBox.Show("Installer object created.");
    

다음 단계

ClickOnce 애플리케이션은 업데이트 프로세스 중에 표시할 사용자 지정 사용자 인터페이스를 포함하여 사용자 지정 업데이트 논리를 추가할 수도 있습니다. 자세한 내용은 UpdateCheckInfo를 참조하세요. ClickOnce 애플리케이션은 <customUX> 요소를 사용하여 표준 시작 메뉴 항목, 바로 가기 및 프로그램 추가/제거 항목을 표시하지 않을 수도 있습니다. 자세한 내용은 <entryPoint> 요소ShortcutAppId를 참조하세요.