How to auto update your application
May 21st, 2008
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) // download the exe if (shouldContinue) // copy over running app // delete any old archived apps, archive the current app // restart the app
{
string downloadedFilePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), “bitter.exe”);
bool shouldContinue = true;
try
{
WebClient wc = new WebClient();
wc.DownloadFile(applicationDownloadURL, downloadedFilePath);
}
catch (Exception ex)
{
shouldContinue = false;
}
{
// get running app path
string appPath = Application.ExecutablePath;
try
{
// create archive’s app path
string archiveAppPath = appPath + “.old”;
// 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;
}
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!





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! :>
May 21, 2008 @ 2:28 pm
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
May 21, 2008 @ 3:07 pm
[…] How to Auto Update Your Application (Mike Hall) […]
May 22, 2008 @ 9:24 am
[…] How to auto update your application - Mike Hall shows how easily you can implement auto updates in your .NET applications. […]
May 23, 2008 @ 2:17 am
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.
May 23, 2008 @ 8:46 am
Stephen, that’s a good point. I forgot about how ClickOnce doesn’t give you that option.
May 23, 2008 @ 12:11 pm
[…] How to auto update your application […]
May 24, 2008 @ 10:13 am
I didn’t know that you could rename a running application’s executable path, thanks for the sample!
May 26, 2008 @ 2:10 am
[…] 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 […]
May 27, 2008 @ 1:13 am
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…
June 13, 2008 @ 4:09 pm