这个应用程序是如何更新的?

时间:2012-09-16 11:59:56

标签: c#

我一直在寻找一种方法来更新我的应用程序多年,但仍然没有找到解决方案。 (请不要说ClickOnce,它不适合这个应用程序。)

多年前,我曾经使用MCadmin运行Minecraft服务器,我记得当它启动时,有时只会说“更新下载,请重启!”。我试图找出这是如何完成的,所以我一直在寻找源代码并找到了一些东西。

以下是我发现的一些代码:

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);
            }

        }

此代码来自名为“UpdateManager”的类中的单个void。

了解它如何处理整个“MCadmin.exe.old”和“MCadmin.exe.new”文件,有点像影子复制。

更新程序代码还有更多内容,但我不太明白。

这是SVN: https://code.google.com/p/mcadminfork/source/browse/

有人能帮助我了解这个更新程序是如何实现的吗?

感谢。

1 个答案:

答案 0 :(得分:2)

Util.DownloadURLToAndDiff()执行实际下载和文件比较。所以你可能想看一下。

否则,它非常简单:

  1. 下载MCAdmin.exe.new
  2. 删除MCAdmin.exe.old(从以前的更新中删除)
  3. 尝试删除当前的MCAdmin.exe
  4. 如果删除失败(可能正在使用文件),请重命名MCAdmin.exe MCAdmin.exe.old
  5. 重命名MCAdmin.exe.new MCAdmin.exe