سؤال

أحاول إنشاء برنامج نصي سوف "التحديث الذاتي" عند اكتشاف إصدار جديد على الخادم.في البداية، كانت الفكرة التي كان لديها، عند اكتشاف إصدار جديد، قم بتنزيل الملف، ثم بدء تشغيل ملف دفعة DOS يقوم ببساطة الكتابة فوق EXE الأصلي مع الجديد.اعتقدت أنه يمكنني استخدام Sytem (بدء تشغيل. BAT) ثم الخروج فورا 0؛ينتظر ملف .bat بضع ثوان، ثم يحاول حذف EXE القديم.هذا فشل، أعتقد لأنه حتى عند استخدام النظام (ابدأ ...)، فإن "العملية" الجديدة هي في الواقع نفس العملية، هل هذا صحيح؟هل هناك أي طريقة لإطلاق عملية جديدة تماما من بيرل في Windows، من شأنها أن تسمح لي بحذف .EXE؟أم أن هناك نهج مختلف سيكون أفضل؟

شكرا، إريك سيفرت

هل كانت مفيدة؟

المحلول

See the documentation on the Windows Restart Manager, an API introduced with Windows Vista that manages restarting applications, for updates, for you.

نصائح أخرى

Ever heard of java WebStart? There is a PAR equivalent, PAR::WebStart Basically, you configure it, then download code live from a trusted website

so your .exe would just be a frontend to PAR::WebStart, with the real program downloaded off your website

I know this is an old question, but I thought I'd put my solution here since I'm working on the same sort of thing. Your batch idea is the way I went, but the small change I made was to use exec() instead of system() since the former (according to this) "...executes a system command and never returns", which seems to release the running process. This appears to work both when running interpreted Perl files with Strawberry Perl, and when running compiled programs with PAR::Packer.

I've created test.pl, which was ultimately compiled to test.exe, as follows:

#!/usr/bin/perl

use 5.10.0;
use strict;
use warnings;

    my $batch_file = <<"BATCH";
rem ### IT IS SAFE TO DELETE THIS FILE ###

\@echo off
ping 127.0.0.1 -n 2 -w 1000 > nul
echo Copying over update file...
copy testfile.exe test.exe

BATCH

    open my $fh, '>', 'update.bat';
    print $fh $batch_file;
    close $fh;

    exec( 'update.bat' );

testfile.exe is a standby "upgraded" version of test.exe, just to make sure that I can overwrite test.exe while it's running, and I can. The ping is a way to pause for 2 seconds to make sure the process has had a chance to exit before trying to overwrite the file.

Oddly enough, the created batch file update.bat can't delete itself, even though batch files can normally delete themselves. If I create a batch file with this:

start /b "" cmd /c del %0

It will delete itself without any errors. But if I include that snippet in update.bat, it complains that The process cannot access the file because it is being used by another process. I'm not sure why. Since this isn't particularly important in my application, I haven't pursued this. I just leave update.bat behind and let it be overwritten next time an update occurs. But I do notate in the file that it's safe to delete in case anyone looks at it later.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top