Saturday, October 10, 2009

ClickOnce Install Actions

The following code class is designed to provide the ability to execute installing a Visual Studio Tools for Office (VSTO) into the ClickOnce cache.

This code would be desired for use by an MSI based installer or a utility tool.

*************************************************

using System;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Security;
using System.Security.Permissions;
using System.Text;

namespace ClickOnceCustomActions
{
    public static class ClickOnceActions
    {
        public static bool CheckForPermission()
        {
            bool bRtn = false;

            try
            {
                SecurityPermission permission =
                    new SecurityPermission(PermissionState.Unrestricted);
                permission.Demand();

                bRtn = true;
            }
            catch (SecurityException)
            {
                throw new InstallException(
                    "You have insufficient privileges to " +
                    "install the add-in into the ClickOnce cache. " +
                    "Please contact your system administrator.");
            }

            return bRtn;
        }

        public static void InstallVSTO(string fileAbsolutePath)
        {
            string arguments = String.Format(
                        "/S /I \"{0}\"", fileAbsolutePath);

            int exitCode = ExecuteVSTOInstaller(arguments);
            if (exitCode != 0)
            {
                string message = null;
                switch (exitCode)
                {
                    case -300:
                        message = String.Format(
                            "The Visual Studio Tools for Office solution " +
                            "was signed by an untrusted publisher and as " +
                            "such cannot be installed automatically. " +
                            "Please use your browser to navigate to {0} in " +
                            "order to install the solution manually. " +
                            "You will be prompted if the solution is trusted for execution.",
                            fileAbsolutePath);
                        break;

                    default:
                        message = String.Format(
                            "The installation of the ClickOnce solution failed with exit code {0}",
                            exitCode);
                        break;
                }
                throw new InstallException(message);
            }
        }

        public static void UninstallVSTO(string fileAbsolutePath)
        {
            string arguments = String.Format(
                "/S /U \"{0}\"", fileAbsolutePath);

            ExecuteVSTOInstaller(arguments);
        }

        public static int ExecuteVSTOInstaller(string arguments)
        {
            string basePath = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);
            string subPath = @"Microsoft Shared\VSTO\9.0\VSTOInstaller.exe";
            string vstoInstallerPath = Path.Combine(basePath, subPath);

            if (File.Exists(vstoInstallerPath) == false)
            {
                throw new InstallException(
                    "The Visual Studio Tools for Office installer was not found.");
            }

            ProcessStartInfo startInfo = new ProcessStartInfo(vstoInstallerPath);
            startInfo.Arguments = arguments;

            Process process = Process.Start(startInfo);
            process.WaitForExit();

            int exitCode = process.ExitCode;
            return exitCode;
        }
    }
}

No comments:

Post a Comment