研究了一下,按上面6#思路做了个例子,图方便没有用API来读写文件,如果能用API来读写,效率要高很多,不过,这里只是提供思路,就懒得用API了;需编译后才能正常运行,因更改文件有2秒左右延时,
;所以测试时,关闭程序后,需2秒以后重新运行才能正常显示结果
Dim $backupfile = @TempDir &"\test0.exe",$stoptimes = 3
$x = _count ()+1
If $x > 4 Then
MsgBox (16,"","运行已达"&$stoptimes&"次")
Exit
Else
MsgBox (0,"","第"&$x&"次运行本程序")
EndIf
If @Compiled Then _Selfcopy (2)
Func _count()
If @Compiled Then
FileCopy ("test2.exe",$backupfile,1)
$SelfSize = FileGetSize ($backupfile)
$hOF = FileOpen ($backupfile,16)
FileRead ($hOF,$SelfSize-12)
$count = FileRead ($hOF,12)
FileClose ($hOF)
$scount = BinaryToString ($count)
If Not StringInStr ($scount,"count") Then
$newcount = StringToBinary("count0")
$fwreturn = FileWrite(FileOpen($backupfile, 17), $newcount)
Return 0
ElseIf StringInStr ($scount,"count") Then
$times = StringRight($scount,1)
If $times > $stoptimes-2 Then
Return 10
Else
$times += 1
$hOF = FileOpen($backupfile, 17)
FileWrite($hOF, StringToBinary ($times))
FileClose($hOF)
EndIf
Return $times
EndIf
EndIf
EndFunc
Func _Selfcopy($iDelay = 0)
Local $sCmdFile
FileDelete(@TempDir & "\scratch.bat")
$sCmdFile = 'ping -n ' & $iDelay & '127.0.0.1 > nul' & @CRLF _
& ':loop' & @CRLF _
& 'copy /y "'& $backupfile &'" "' & @ScriptFullPath & '" > nul' & @CRLF _
& 'del ' & $backupfile & @CRLF _
& 'del ' & @TempDir & '\scratch.bat'
FileWrite(@TempDir & "\scratch.bat", $sCmdFile)
Run(@TempDir & "\scratch.bat", @TempDir,@SW_HIDE)
EndFunc
|