基本原理在某个服务器上共享出一个目录,其权限为程序员完全控制,其它用户只读。
例如:
在应用程序的入口处调用版本检查及更新过程,假如服务器应用程序的修改时间大于本地应用程序的修改时间,则认为有新版本出现,应该将服务器上的新版本复制到本地硬盘。
由于应用程序在运行时不能被新版本覆盖,所以就需要中介程序
本例中:
服务器
应用程序
下载源代码(6K)
在应用程序工程MyApp中的部分代码如下:
OptionExplicit
PrivateConstApp_Name=%26quot;MyApp%26quot;
PrivateConstExePath=%26quot;\\NtServer01\Refresh\%26quot;
PrivateConstMidExeName=%26quot;FastCopy%26quot;
PrivateSubForm_Load()
IfUCase(Trim(App.EXEName))%26lt;%26gt;UCase(Trim(App_Name))Then
MsgBox%26quot;必须将订单治理系统的名称更改为:%26quot; App_Name
End
EndIf
CallExeRefresh
EndSub
PrivateSubExeRefresh()
Dims1AsString
Dims2AsString
Dims3AsString
Dims4AsString
OnErrorResumeNext
'将本地中介程序FastCopy.exe的全路径名存入s3
s1=%26quot;TNT%26quot;
IfLen(App.Path)%26gt;3Then
s1=App.Path %26quot;\%26quot; Trim(App_Name) %26quot;.exe%26quot;
s3=App.Path %26quot;\%26quot; MidExeName %26quot;.EXE%26quot;
Else
s1=App.Path Trim(App_Name) %26quot;.exe%26quot;
s3=App.Path MidExeName %26quot;.EXE%26quot;
EndIf
s4=%26quot;TNT%26quot;
s4=FileDateTime(s1)
s2=%26quot;TNT%26quot;
s2=FileDateTime(ExePath App_Name %26quot;.EXE%26quot;)
Ifs2=%26quot;TNT%26quot;Then
MsgBox%26quot;没有找到最新的可执行文件:%26quot; ExePath App_Name %26quot;.EXE%26quot;_
vbCrLf vbCrLf %26quot;原因1:存放最新EXE的服务器或者工作站没有打开;%26quot;_
vbCrLf %26quot;原因2:存放最新EXE的路径错误或者EXE文件不存在;%26quot;_
vbCrLf %26quot;请将此情况通知程序员.%26quot; vbCrLf vbCrLf_
vbCrLf %26quot;按确定按钮后,将继续运行本地EXE文件.%26quot;,vbCritical,%26quot;提示%26quot;
EndIf
Ifs2=%26quot;TNT%26quot;Ors4=%26quot;TNT%26quot;ThenExitSub
'然后再运行本地MyApp.EXE,中介程序退出后,整个更新过程结束.
IfCDate(s2)%26gt;CDate(s4)Then
FileCopyExePath MidExeName %26quot;.EXE%26quot;,s3
s1=Shell(s3 %26quot;%26quot; ExePath %26quot;,%26quot; App_Name %26quot;.EXE%26quot;,vbNormalFocus)
End
EndIf
EndSub
将以上程序编译为:MyApp.exe存储在共享目录中.
向工程中增加一个窗体Form1,向Form1中添加一个定时器Timer1,增加一个标签控件Label1,其Caption为%26quot;应用程序正在更新%26quot;,并调整窗体大小.
OptionExplicit
PrivatesPathAsString
PrivatesNameAsString
PrivateSubForm_Load()
DimsAsString
s=Trim(Command())
DimpAsInteger
p=InStr(1,s,%26quot;,%26quot;)
Ifp%26gt;0Then
sPath=Mid(s,1,p-1)
sName=Mid(s,p 1,Len(s)-p)
Timer1.Interval=6000
Else
MsgBox%26quot;Error%26quot;,vbCritical,%26quot;%26quot;
UnloadMe
End
EndIf
EndSub
PrivateSubTimer1_Timer()
Timer1.Interval=0
Dims1AsString
IfLen(App.Path)%26gt;3Then
s1=App.Path %26quot;\%26quot; sName
Else
s1=App.Path sName
EndIf
OnErrorResumeNext
FileCopysPath sName,s1
DimaAsLong
a=Shell(s1,vbNormalFocus)
UnloadForm1
End
EndSub
将工程FastCopy编译为FastCopy.exe并存储于服务器的共享目录。
1、将服务器共享目录中的MyApp.exe复制到本地硬盘的某个目录中;
2、重新编译MyApp工程,将MyApp.exe复制到服务器的共享目录中,注重一定不要覆盖本地的MyExe.app;
3、这样服务器上MyApp.exe的修改时间肯定大于本地MyApp.exe的修改时间;
4、运行本地MyApp.exe,几秒钟后屏幕上会出现%26quot;应用程序正在更新%26quot;的窗体,随后更新后应用程序再次被运行。则自动更新成功。
5、程序的修改时间可通过在文件上用鼠标按右键的属性或内容的菜单观察。
本程序在Windows98和VB6.0,NT局域网络下测试通过。
本文内容也适用于其它语言参考。->
