rulururu

post How to auto update your application

May 21st, 2008

Filed under: coding, programming, usability — mike hall @ 1:14 am

In my quest to make my Twitter client Bitter as easy to use as possible, one upgrade I had considered was making it auto-updateable. You click a button, it updates. Done.

As most things in .NET, it turned out to be relatively simple. You download the file, you move it on top of the running app, you restart… all neat and tidy. So without further ado, here’s the code:


private void UpdateApplication(string applicationDownloadURL)
{
    string downloadedFilePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), “bitter.exe”);

    // download the exe
    bool shouldContinue = true;
    try
    {
        WebClient wc = new WebClient();
        wc.DownloadFile(applicationDownloadURL, downloadedFilePath);
    }
    catch (Exception ex)
    {
        shouldContinue = false;
    }

    if (shouldContinue)
    {
        // get running app path
        string appPath = Application.ExecutablePath;

        // copy over running app
        try
        {
            // create archive’s app path
            string archiveAppPath = appPath + “.old”;

            // delete any old archived apps, archive the current app
            // and move the downloaded app in place
            System.IO.File.Delete(archiveAppPath);
            System.IO.File.Move(appPath, archiveAppPath);
            System.IO.File.Move(downloadedFilePath, appPath);
        }
        catch (Exception ex)
        {
            shouldContinue = false;
        }

        // restart the app
        if (shouldContinue)
        {
            Application.Restart();
        }
    }

}

Of course you should add logging and user feedback where appropriate. This is just skeleton code that gets the job done. Enjoy!

10 Comments »

  1. I don’t mean to steal your thunder, but ClickOnce will do most/all of this heavy lifting for you. I just learned about it at 2008 DevTeach Toronto. Very worthwhile technology to check out! :>

    Comment by PHenry
    May 21, 2008 @ 2:28 pm

  2. Yeah, ClickOnce will do it, but I’ve heard mixed things about it. And in reality, the code above is only about 10 real lines of any meat. Plus it’s always more fun to roll your own :)

    Comment by mike hall
    May 21, 2008 @ 3:07 pm

  3. […] How to Auto Update Your Application (Mike Hall) […]

    Pingback by Dew Drop - May 22, 2008 | Alvin Ashcraft's Morning Dew
    May 22, 2008 @ 9:24 am

  4. […] How to auto update your application - Mike Hall shows how easily you can implement auto updates in your .NET applications. […]

    Pingback by Reflective Perspective - Chris Alcock » The Morning Brew #100
    May 23, 2008 @ 2:17 am

  5. Thanks for this tip, Mike. Another way that this is better than ClickOnce is that you have no control over where ClickOnce puts your app — at least that is my experience. I work in an environment where all user apps are required to be installed on a separate partition, so ClickOnce is rarely an option for us.

    Comment by Stephen McMahon
    May 23, 2008 @ 8:46 am

  6. Stephen, that’s a good point. I forgot about how ClickOnce doesn’t give you that option.

    Comment by mike hall
    May 23, 2008 @ 12:11 pm

  7. […] How to auto update your application […]

    Pingback by Interesting Finds: 2008.05.24 - gOODiDEA.NET
    May 24, 2008 @ 10:13 am

  8. I didn’t know that you could rename a running application’s executable path, thanks for the sample!

    Comment by Hüseyin Tüfekçilerli
    May 26, 2008 @ 2:10 am

  9. […] and restart the application. This is what I’ve currently done with Bitter as I document in this post. This is still not that hard to accomplish. In .NET, I showed that doing this is only about 10 […]

    Pingback by i like ellipses… » How Should You Update Your Application?
    May 27, 2008 @ 1:13 am

  10. That’s awesome you can rename the running executable. Is the same thing possible with all DLLs that executable might have referenced? I’m guessing so…

    Comment by Shane Milton
    June 13, 2008 @ 4:09 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

ruldrurd

Powered by WordPress, Theme based off the "I'm Okay" theme by Laurentiu Piron

Creative Commons License This work is licensed under a Creative Commons Attribution 3.0 United States License.


Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.