How to: Respond to ClickOnce Publish Events

Visual Studio add-ins are deprecated in Visual Studio 2013. You should upgrade your add-ins to VSPackage extensions. For more information about upgrading, see FAQ: Converting Add-ins to VSPackage Extensions.

ClickOnce enables you to publish Windows applications to a Web server or network file share for simplified installation. For more information, see ClickOnce Security and Deployment.

The Visual Studio core automation model (contained in EnvDTE80) has an event-handling object named PublishEvents. You can use it to detect when a ClickOnce deployment begins (OnPublishBegin) and ends (OnPublishDone). Based on these events, you can perform an action. The following procedure demonstrates how to handle those events.

Note Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Customizing Development Settings in Visual Studio.

To respond to ClickOnce events

  1. In a new or existing add-in, add the following code to the Connect class.

  2. Build and activate the add-in as outlined in How to: Control Add-Ins By Using the Add-In Manager.

  3. Begin a ClickOnce deployment operation as outlined in ClickOnce Security and Deployment.

    You see a message box display when the deployment begins and ends.

using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
namespace MyAddin2
{
    public class Connect : IDTExtensibility2
    {
        public Connect()
        {
        }

        public void OnConnection(object application,   
           ext_ConnectMode connectMode, object addInInst, ref Array 
           custom)
        {
            _applicationObject = (DTE2)application;
            _addInInstance = (AddIn)addInInst;
            try
            {
                _applicationObject = (DTE2)application;
                _addInInstance = (AddIn)addInInst;
                EnvDTE80.Events2 events;
                events = (Events2)_applicationObject.Events;
                // Retrieve the delegates for the Publish events.
                pubEvents = (EnvDTE80.PublishEvents)
                  events.PublishEvents;
                // Connect to the delegates exposed from the Publish 
                // objects retrieved above.
                pubEvents.OnPublishBegin += new 
                    _dispPublishEvents_OnPublishBeginEventHandler
                    (this.OnPublishBegin);
                pubEvents.OnPublishDone += new 
                    _dispPublishEvents_OnPublishDoneEventHandler
                    (this.OnPublishDone);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        // When the Publish operation is done, disconnect the event 
        // handlers to prevent slowing of your system.
        public void OnDisconnection(ext_DisconnectMode disconnectMode, 
            ref Array custom)
        {
            if (pubEvents != null)
            {
                pubEvents.OnPublishBegin -= new 
                    _dispPublishEvents_OnPublishBeginEventHandler
                    (this.OnPublishBegin);
                pubEvents.OnPublishDone -= new 
                    _dispPublishEvents_OnPublishDoneEventHandler
                    (this.OnPublishDone);
            }
        }

        // The Publish events.
        public void OnPublishBegin(ref bool pubContinue)
        {
            if (pubContinue == true)
            {
                System.Windows.Forms.MessageBox.Show
                    ("A publish event is occuring…");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show
                    ("A publish event has halted.");
            }
        }

        public void OnPublishDone(bool success)
        {
            if (success == true)
            {
                System.Windows.Forms.MessageBox.Show
                    ("A publish event has completed.");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show
                    ("A publish event did not succeed.");
            }
        }
        
        public void OnAddInsUpdate(ref Array custom)
        {
        }

        public void OnStartupComplete(ref Array custom)
        {
        }

        public void OnBeginShutdown(ref Array custom)
        {
        }
        
        private EnvDTE80.PublishEvents pubEvents;
        private DTE2 _applicationObject;
        private AddIn _addInInstance;
    }
}
Show: