i have been looking for a way to update my application for ages, and still haven’t found a solution. (Please don’t say ClickOnce, it isn’t suitable for this app).
Years ago i used to use MCadmin to run a Minecraft server, and i remembered that when it started, sometimes it would just say “Update downloaded, please restart!”. I have tried to find out how this was done, so i have been looking in the source code and found some things.
Here is some code that i found:
private void CheckUpdateThread()
{
Program.AddRTLine(Color.Green, "Verifying existence of essential files...\r\n", false);
if (!File.Exists("ICSharpCode.SharpZipLib.dll"))
Util.DownloadURLToFile("https://internal.mcadmin.eu/ICSharpCode.SharpZipLib.dll", "ICSharpCode.SharpZipLib.dll");
if (!File.Exists("LICENSE.txt"))
Util.DownloadURLToFile("https://internal.mcadmin.eu/LICENSE.txt", "LICENSE.txt");
Program.AddRTLine(Color.Green, "Essential file validation completed!\r\n", false);
if (Program.dontUpdate)
{
Program.AddRTLine(Color.Green, "Update checking disabled!!!\r\n", false);
return;
}
UpdateRunning = true;
Program.AddRTLine(Color.Green, "Checking for updates...\r\n", false);
bool isUpdate;
if (Program.dontUpdateMCAdmin || 1 == 1)
{
Program.AddRTLine(Color.Green, "MCAdmin update checking disabled.\r\n", false);
}
else
{
isUpdate = Util.DownloadURLToAndDiff("https://internal.mcadmin.eu/MCAdmin.exe", "MCAdmin.exe.new", "MCAdmin.exe");
if (!isUpdate)
{
if (OutOfDateMCA)
{
Program.AddRTLine(Color.Orange, "MCAdmin update downloaded! Restart MCAdmin to apply update!\r\n", false);
SendAdminMessage("MCAdmin update downloaded, consider restarting.", 4);
}
else
{
Program.AddRTLine(Color.Green, "MCAdmin already up to date!\r\n", false);
}
}
else
{
try
{
if (File.Exists("MCAdmin.exe.old"))
File.Delete("MCAdmin.exe.old");
}
catch { }
try
{
if (File.Exists("MCAdmin.exe"))
File.Delete("MCAdmin.exe");
}
catch { }
if (File.Exists("MCAdmin.exe"))
File.Move("MCAdmin.exe", "MCAdmin.exe.old");
File.Move("MCAdmin.exe.new", "MCAdmin.exe");
OutOfDateMCA = true;
Program.AddRTLine(Color.Orange, "MCAdmin update downloaded! Restart MCAdmin to apply update!\r\n", false);
SendAdminMessage("MCAdmin update downloaded, consider restarting.", 4);
}
}
This code is from a single void in a class called “UpdateManager”.
See how it does the whole “MCadmin.exe.old” and “MCadmin.exe.new” files, a bit like shadow copying.
There is more to the updater code, but i don’t quite understand.
Here is the SVN:
https://code.google.com/p/mcadminfork/source/browse/
Could anybody help me find out how this updater was acheived?
Thanks.
Util.DownloadURLToAndDiff() does the actual downloading and file comparison. So you probably want to look at that.
Otherwise, it’s pretty simple: