使用WiX制作具有时间限制的安装包

WiX是Windows Installer XML的简称,它是用于制作Windows安装包的工具集。它支持命令行环境,开发者可以及将它集成到他们的编译过程中创建MSI和MSM安装包。

更多信息可以参考:http://wix.sourceforge.net/

 

最近研究了一下如何使用WiX制作具有时间限制的安装包,下图是demo的效果图。

clip_image002

完成这个demo主要有两点值得注意:

1. 选择合适的脚本语言(VBScript or JScript)实现定制操作。

2. 选择合适的实际执行上面的脚本。

 

下面分享我的实现过程:

1. 实现一段检查当前时间是否越界的脚本代码,并将它封装到CustomAction中。

    使用VBScript比较当前日期和限定日期,小于0表示越界,这时调用WScript想注册表添加坏键。

 

 
  
  
  
  
  1. <CustomAction Id="ValidateTimeLimitCA" Script="vbscript" Execute="immediate" > 
  2.     <![CDATA[ 
  3.     If DateDiff("d", Date, #2011/07/31#) < 0 Then 
  4.       Set WShell = CreateObject("WScript.Shell") 
  5.       WShell.RegWrite "HKCU\Software\GrapeCity\TimeLimit\DateDiff", 1, "REG_DWORD" 
  6.       Set WShell = Nothing 
  7.     End If 
  8. ]]> 
  9.   </CustomAction> 

2. 选择合适的时机执行上述脚本,越早越好。

    在本文的demo中,我在PrepareDlg之前执行脚本,对于大多数情况而言,这已经是最早的时机了。当然,你也可以选择在AppSearch之前执行。

 

 
  
  
  
  
  1. <InstallUISequence> 
  2.   <Custom Action="ValidateTimeLimitCA" Before="PrepareDlg" /> 
  3. </InstallUISequence> 

3. 添加一个RegistrySearch属性,用于第一步提到的坏键。

 

 
  
  
  
  
  1. <Property Id="TIMELIMIT"> 
  2.   <RegistrySearch Id="TIMELIMIT_VALUE" Root="HKCU" Key="Software\GrapeCity\TimeLimit" Name="DateDiff" Type="raw" /> 
  3. </Property> 

4. 添加Condition用于检查第三步添加的属性是否存在,并在需要的时候弹出错误信息。

 

 
  
  
  
  
  1. <Condition Message="This is package is out of date at July 31, 2011"> 
  2.     Installed OR NOT TIMELIMIT 
  3.   </Condition> 

5. 实现一个清除操作,用于清除第一步添加的坏键。

 

 
  
  
  
  
  1. <CustomAction Id="CleanupTimeLimitCA" Script="vbscript" Execute="immediate" > 
  2.   <![CDATA[ 
  3.   Set WShell = CreateObject("WScript.Shell") 
  4.   WShell.RegDelete "HKCU\Software\GrapeCity\TimeLimit\" 
  5.   Set WShell = Nothing 
  6. > 
  7. </CustomAction> 

6. 在FatalError对话框中发布Finish事件,同时将值设置给第五步提到的清除操作。

 

 
  
  
  
  
  1. <UI> 
  2.   <UIRef Id="WixUI_Minimal" /> 
  3.   <Publish Dialog="FatalError" Control="Finish" Event="DoAction" Value="CleanupTimeLimitCA">1</Publish> 
  4. </UI> 

 

到这里就完成,代码很简单,希望有价值。

 

源码下载地址如下:

TimeLimit.rar

本文出自 “葡萄城控件博客” 博客,请务必保留此出处http://powertoolsteam.blog.51cto.com/2369428/710316

你可能感兴趣的:(职场,休闲,安装包,WiX)