Wednesday, March 30, 2011

Walkthrough: Creating a Custom Installer for a ClickOnce Application

Visual Studio 2010
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. This walkthrough demonstrates how to create a custom installer that silently installs a ClickOnce application.

To create a custom ClickOnce application installer

In your ClickOnce application, add references to System.Deployment and System.Windows.Forms.
Add a new class to your application and specify any name. This walkthrough uses the name MyInstaller.
Add the following Imports or using statements to the top of your new class.

using System.Deployment.Application;
using System.Windows.Forms;

Add the following methods to your class.

These methods call InPlaceHostingManager methods to download the deployment manifest, assert appropriate permissions, ask the user for permission to install, and then download and install the application into the ClickOnce cache. A custom installer can specify that a ClickOnce application is pre-trusted, or can defer the trust decision to the AssertApplicationRequirements method call. This code pre-trusts the application.

Note
Permissions assigned by pre-trusting cannot exceed the permissions of the custom installer code.

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?";

Read more: MSDN