Share via


How to: Respond to ClickOnce Publish Events

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

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 ExportSettings on the Tools menu. For more information, see Visual Studio Settings.

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.

Example

Public Class Connect
    Implements IDTExtensibility2
    Public WithEvents pubEvents As EnvDTE80.PublishEvents

    Dim _applicationObject As DTE2
    Dim _addInInstance As AddIn

    Public Sub OnConnection(ByVal application As Object, ByVal _
    connectMode As ext_ConnectMode, ByVal addInInst As Object, ByRef _
    custom As Array) 
    Implements IDTExtensibility2.OnConnection
        Try
        _applicationObject = CType(application, DTE2)
        _addInInstance = CType(addInInst, AddIn)
        Try
            _applicationObject = CType(application, DTE2)
            _addInInstance = CType(addInInst, AddIn)
            Dim events As EnvDTE80.Events2
            events = CType(_applicationObject.Events2, Events2)
            pubEvents = CType(events._PublishEvents(Nothing), _
            EnvDTE80.PublishEvents)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub pubEvents_OnPublishBegin(ByRef [Continue] As Boolean) _
    Handles pubEvents.OnPublishBegin
        MsgBox("A publish event is occuring…")
    End Sub

    Private Sub pubEvents_OnPublishDone(ByVal Success As Boolean) _
    Handles pubEvents.OnPublishDone
        MsgBox("A publish event has completed.")
    End Sub

    Public Sub OnDisconnection(ByVal disconnectMode As _
      ext_DisconnectMode, ByRef custom As Array) Implements _
      IDTExtensibility2.OnDisconnection
        pubEvents = Nothing
    End Sub
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;
    }
}

See Also

Concepts

Controlling Projects and Solutions

Other Resources

Responding to Automation Events