Using the ApplicationDeployment Class in Visual Basic

Introduction

Welcome to my article. Writing a program is only one little cog in the development wheel. The whole purpose of developing any application is to make the end user’s life easier. Knowing how to deploy your application is vital to the end user’s experience and happiness. Today, you will play around with the ApplicationDeployment class in the .NET Framework.

Software Deployment

Software deployment is all of the various activities that make a software system available for use for the end user. These activities are listed below.

Release

The release activity determines all resources required to operate on the site as well as collect necessary information for carrying out subsequent activities of deployment process.

Install and Activate

This is the activity of starting up the executable (application) of software.

Deactivate

Deactivation is the opposite of activation and refers to shutting down any running components of the executable (application).

Adapt

This activity is a process to modify a software system that has been previously installed.

Update

The update process simply replaces an earlier version of all or part of the particular software application with a newer release. There are two options here:

  • Built-In: Mechanisms for installing updates are built into some software systems.
  • Version tracking: Version tracking systems help users find and install updates to applications installed on their PCs or local networks.

Uninstall

Uninstallation is the opposite of installation. It is the removal of an application that is no longer required.

Retire

This happens when a software application is marked as obsolete, or support by the software producers is withdrawn. This is the end of the life cycle of a software product.

Types of Deployment

Deployment via Distributable Media: A Windows Installer gets created, which then can be distributed on CDs, DVDs, or even on a network drive.

Deployment of a Web Setup Project

You can create and deploy an application for download from a Web server.

Merge Modules

Merge Module projects allow you to create reusable setup components.

ClickOnce

ClickOnce deployment allows you to publish Windows-based applications to a Web server.

Now that you know more about Deployment, let’s have a look at the ApplicationDeployment Class.

The ApplicationDeployment Class

The ApplicationDeployment class supports updates of the current deployment programmatically, and handles on-demand downloading of files. For a detailed explanation as well as a list of properties and methods of the ApplicationDeployment class, refer to MSDN.

A Small Project

Let’s create a small Visual basic Windows Forms Project. This project will be used to determine whether or not there are updates available for your application. If there are, it will automatically download and install them.

Once you have created the project skeleton, add a textbox to your form.

Code

Add the following two namespaces:

Imports System.Deployment.Application
Imports System.ComponentModel

The System.Deployment.Application namespace allows you to program custom upgrade behaviour into your ClickOnce application. The System.ComponentModel namespace provides several classes that can be used to implement the run-time and design-time behaviour of your components or your controls.

Add the following variables to your ode:

   Private lngUpdateSize As Long = 0

   Dim WithEvents adUpdate As ApplicationDeployment

lngUpdateSize determines the size of the potential download. adUpdate is our ApplicationDeployment object. I have declared it ‘WithEvents’ because we will make use of its events to determine updates, download updates, track updates, and install updates.

Add the Update sub that determines how the target application has been deployed:

   Private Sub UpdateApplication(

      If (ApplicationDeployment.IsNetworkDeployed) Then
         adUpdate = ApplicationDeployment.CurrentDeployment

         adUpdate.CheckForUpdateAsync()
      End If

   End Sub

Determine how big the Update is (if one is available):

   Private Sub adUpdate_CheckForUpdateProgressChanged(ByVal _
      sender As Object, ByVal e _
      As DeploymentProgressChangedEventArgs) _
      Handles adUpdate.CheckForUpdateProgressChanged

      txtUpdateStatus.Text = [String].Format("{0:D}K of _
         {1:D}K downloaded.", e.BytesCompleted / _
         1024, e.BytesTotal / 1024)

   End Sub

   Private Sub adUpdate_CheckForUpdateCompleted(ByVal sender _
      As Object, ByVal e As CheckForUpdateCompletedEventArgs) _
      Handles adUpdate.CheckForUpdateCompleted

      If (e.Error IsNot Nothing) Then

         MessageBox.Show(("ERROR"))

         Return

      Else

         If (e.Cancelled = True) Then

            MessageBox.Show("The update was cancelled.")

         End If

      End If


      If (e.UpdateAvailable) Then

         lngUpdateSize = e.UpdateSizeBytes

         If (Not e.IsUpdateRequired) Then

            Dim dr As DialogResult = MessageBox.Show("An update is _
               available, Update now?", "Update Available", _
               MessageBoxButtons.OKCancel)
            If (System.Windows.Forms.DialogResult.OK = dr) Then

               BeginUpdate()

            End If

         Else

            MessageBox.Show("A mandatory update is available and _
               will be installing now")

            BeginUpdate()

         End If
      End If

   End Sub

Begin the Update:

   Private Sub BeginUpdate()

      adUpdate = ApplicationDeployment.CurrentDeployment
      adUpdate.UpdateAsync()

   End Sub

Show the progress of the update that is being downloaded and installed:

   Private Sub adUpdate_UpdateProgressChanged(ByVal _
      sender As Object, ByVal e As _
      DeploymentProgressChangedEventArgs) Handles _
      adUpdate.UpdateProgressChanged

      Dim strProgress As String = String.Format("{0:D}K out _
         of {1:D}K downloaded - {2:D}% complete", e.BytesCompleted / _
         1024, e.BytesTotal / 1024, e.ProgressPercentage)

      txtUpdateStatus.Text = strProgress

   End Sub


   Private Sub adUpdate_UpdateCompleted(ByVal sender As Object, _
      ByVal e As AsyncCompletedEventArgs) _
      Handles adUpdate.UpdateCompleted

      If (e.Cancelled) Then

         MessageBox.Show("The update was cancelled.")

         Exit Sub

      Else

         If (e.Error IsNot Nothing) Then

            MessageBox.Show("ERROR")

            Exit Sub

         End If

      End If

      Dim drUpdate As DialogResult = MessageBox.Show("The application _
         has been updated. Restart?", "Restart Application", _
         MessageBoxButtons.OKCancel)

      If (drUpdate = System.Windows.Forms.DialogResult.OK) Then

         Application.Restart()

      End If

   End Sub

Remember to use your Form_Load event or a button event; for example, to get the update process started. In the next code segment, I call the BeginUpdate procedure inside Form_Load:

   Private Sub Form1_Load(sender As Object, _
      e As EventArgs) Handles MyBase.Load

      UpdateApplication()

   End Sub

I am attaching this app as a sample for you with this article.

Conclusion

Knowing how to deploy your applications properly comes with a lot of planning and is vital for any developer. Until next time, cheers!

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read