rictirse 發表於 2013-2-15 00:17

AutoIt 撰寫的自動更新程序,各軟體皆適用

本帖最後由 rictirse 於 2014-5-27 10:40 編輯



可自動偵測本機內的檔案大小,是否與伺服器端的檔案相同
如不相同會自動啟動更新作業,可適用各種軟體更新作業#include <GUIConstants.au3>
#include <Misc.au3>
Opt ("GUIOnEventMode", 1)

Dim $Title = "AutoUpdata"
Dim $FilePath_Server = "http://xxxxxxxxxxx";;伺服器新檔案
Dim $FilePath_Local = "R:\run.exe" ;;需要更新程式的路徑

_Singleton ($Title);;僅允許程式單獨執行

;~ _Check();;前置檢查作業

GUICreate ( $Title, 280, 80)

GUICtrlCreateLabel ("更新進度", 10, 20)

$Progres = GUICtrlCreateProgress ( 70, 15, 200, 20)
GUICtrlCreateButton ("開始更新", 70, 50, 60, 20)
GUICtrlSetOnEvent ( -1, "_Start")
GUICtrlCreateButton ( "離開", 150, 50, 60, 20)
GUICtrlSetOnEvent ( -1, "_Exit")

GUISetState ()

While True
Sleep (50)
WEnd

Func _Exit()
Exit
EndFunc

Func _Start()
$TotalSize = InetGetSize ($FilePath_Server) ;; 取得總容量
$hDownload = InetGet ($FilePath_Server, $FilePath_Local, 1, 1) ;;開始下載

Do
    Sleep (50)
    $NowDownload = InetGetInfo($hDownload, 0)

    $per = Int ($NowDownload/$TotalSize*100) ;;計算百分比
    $res = Int (StringLeft ( $per, 3)) ;;百分值
    GUICtrlSetData ($progres , $res) ;;下載進度

    TrayTip ( "正在下載更新中", "已下載 " & ($NowDownload/1024)&" kB", 1) ;;監視已下載大小

Until InetGetInfo ( $hDownload, 2)

GuiCtrlSetData ( $progres , 0)

If $TotalSize = FileGetSize ($FilePath_Local) Then
    MsgBox ( 0, "更新完成", "檔案大小:" &Int ($TotalSize/1024)&" kB"&@CRLF&"已下載大小:" & Int ($NowDownload/1024)&" kB")
    Run($FilePath_Local) ;;執行程序
    Exit
Else
    MsgBox ( 16, "錯誤", "更新失敗。")
EndIf

EndFunc

Func _Check()
$Ping = Ping ( "8.8.8.8", 500) ;;;檢查網路是否通暢

If $Ping Then
    TrayTip ( "自動更新程序", "正在檢查檔案...", 2)
Else
    MsgBox ( 16, "警告", "網路連結失敗,請稍後再嘗試。")
    Exit
EndIf

$FileSize_Server = InetGetSize ($FilePath_Server);;取得伺服器新檔案的大小
If @error Then
    MsgBox ( 36, "警告", "無法取得伺服器訊息,")
    Exit
EndIf

$FileSize_Local = FileGetSize ("run.exe");;取得本機檔案大小
If @error Then
    If MsgBox ( 36, "警告", "無法取得檔案資訊,是否繼續下載。") = 7 Then Exit
EndIf

If $FileSize_Server = $FileSize_Local Then
      MsgBox( 0, "提示", "檔案檢查完成"&@CRLF&"目前已為最新版。")
      Exit
EndIf
EndFunc

bunnie 發表於 2013-2-15 00:38

噹噹,
你利用檔案大小來判斷很不錯!
我都是寫一個ini到檔案裡面,
之後匯入到使用者電腦中,
開啟程式後,
它會自動跟server端的版本做比對..

rictirse 發表於 2013-2-15 02:18

本帖最後由 rictirse 於 2014-5-27 10:40 編輯

bunnie 發表於 2013-2-15 00:38 static/image/common/back.gif
噹噹,
你利用檔案大小來判斷很不錯!
我都是寫一個ini到檔案裡面,

其實用MD5去判斷會更準確
只是我不太確定Server上面的是不是可以產生MD5
不然就是要先手動產生放在XML上
然後用 _MD5(@ScriptFullPath) 產生MD5 去跟 網路上的MD5做比對
這是比較複雜的方法

附上MD5 UDF
$sFile = @WindowsDir & "\Explorer.exe"
MsgBox (0,"", _MD5($sFile))

Func _MD5($sFile)
Local Const $CALG_MD2 = 0x8001
Local Const $CALG_MD4 = 0x8002
Local Const $CALG_MD5 = 0x8003
Local Const $CALG_SHA1 = 0x8004

$hFile = FileOpen ($sFile, 16)
$bData = FileRead ($hFile)
$iLength = BinaryLen ($bData)
FileClose($hFile)

$tBuffer = DllStructCreate("byte[" & $iLength & "]")
$pBuffer = DllStructGetPtr($tBuffer)
DllStructSetData($tBuffer, 1, $bData)

$iResult = DllCall ("Crypt32.dll", "int", "CryptHashCertificate", "hWnd", 0, "dword", $CALG_MD5, "dword", 0, "ptr", $pBuffer, "dword", $iLength, "ptr", 0, "dword*", 0)
$tResult = DllStructCreate ("byte[" & $iResult & "]")
$pResult = DllStructGetPtr ($tResult)
$iResult = DllCall ("Crypt32.dll", "int", "CryptHashCertificate", "hWnd", 0, "dword", $CALG_MD5, "dword", 0, "ptr", $pBuffer, "dword", $iLength, "ptr", $pResult, "dword*", $iResult)
If $iResult <> 0 Then Return StringTrimLeft (DllStructGetData ($tResult, 1), 2)

$tBuffer = 0
$tResult = 0
EndFunc

xx910203 發表於 2013-2-15 02:45

頁: [1]
查看完整版本: AutoIt 撰寫的自動更新程序,各軟體皆適用