Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / Visual Basic

Adding automatic updates to your program - Part 1

Rate me:
Please Sign up or sign in to vote.
4.86/5 (64 votes)
31 Dec 2007CPOL6 min read 1.1M   11.7K   430  
This article describes the steps to add automatic update capabilities to your application quickly and easily using the DDay.Update library.
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Windows.Forms;
using System.Threading;

using DDay.Update.Utilities;
using DDay.Update.Configuration;

[[AssemblyAttributes]]

namespace DDay.Update.Bootstrap
{
    class Program
    {
        static private ILog log = new Log4NetLogger();

        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

            // Get the DDay.Update configuration section
            DDayUpdateConfigurationSection cfg = ConfigurationManager.GetSection("DDay.Update")
                as DDayUpdateConfigurationSection;

            // Set the command line parameters to the application
            UpdateManager.SetCommandLineParameters(args);

            try
            {
                // Determine if an update is available
                if (UpdateManager.IsUpdateAvailable(cfg.Uri))
                {
                    UpdateManager.UpdateCompleted += new EventHandler(UpdateManager_UpdateCompleted);
                    UpdateManager.UpdateCancelled += new EventHandler(UpdateManager_UpdateCompleted);
                    UpdateManager.UpdateError += new EventHandler<ExceptionEventArgs>(UpdateManager_UpdateError);
                    log.Debug("Update is available, beginning update process...");

                    // Perform the update, which performs the following steps:
                    // 1. Updates to a new version of the application
                    // 2. Saves new deployment and application manifests locally                    

                    if (UpdateManager.Update())
                        Application.Run();
                    else
                    {
                        // User declined to update the application,
                        // or the update is otherwise not going
                        // to happen.
                        UpdateManager.StartApplication();
                    }
                }
                else
                {
                    log.Debug("Application is up-to-date.");
                    UpdateManager.StartApplication();
                }
            }
            catch
            {
                // Start the application
                UpdateManager.StartApplication();
            }
        }

        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            RunApplication();
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            RunApplication();
        }

        static void UpdateManager_UpdateError(object sender, ExceptionEventArgs e)
        {
            RunApplication();
        }

        static void UpdateManager_UpdateCompleted(object sender, EventArgs e)
        {
            RunApplication();
        }

        static void RunApplication()
        {
            // Start the application
            UpdateManager.StartApplication();
                        
            log.Debug("Exiting bootstrap application...");
            Application.Exit();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Doug has been a Software Engineer for 7 of the previous 9 years, and has 12 years of programming experience.

For the past 3 years, he has been developing custom applications in C# and Visual Basic.NET, with an emphasis on custom cross-Internet applications for IT management, real-time collaboration, and process management and reporting.

Comments and Discussions