2012-09-16 21 views
-2

我一直在尋找一種方式來更新我的應用程序的年齡,並且還沒有找到解決方案。 (請不要說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」類單無效。

看看它如何處理整個「MCadmin.exe.old」和「MCadmin.exe.new」文件,有點像影子複製。

還有更多的代碼,但我不太明白。

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

任何人可以幫我找出這個更新是如何來達到的?

謝謝。

+0

你確切的問題是什麼?你在代碼中看到,程序再次下載它自己的代碼庫並將其替換爲...(這沒有檢查簽名具有很高的安全風險...) – user287107

回答

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
+0

謝謝! - 我很難理解它,現在就清除它! – user1588364

+0

沒問題。如果這回答了您的問題,請將其標記爲接受的答案(http://stackoverflow.com/faq)。 –

+0

好的,哦,還有一件事,我繼續主要的表單加載事件,並且它不啓動更新,其中如果不存在,那麼所有的啓動事件都會發生? – user1588364

相關問題