默认的安装目录是C盘的ProgramFile文件夹,当下次进行安装(使用其它版本进行升降级)时,我们希望能够获得上一次的选择位置。
默认的下载目录也是类似,只是还有更多的东西需要实现:写个界面来获取输入,以及在解压完文件后在config/app.config
输出配置结果。
以上这些操作其实与jpackage
关系不大,主要的关键词是Wix
。
保存安装目录
建立INSTALLDIR Property,这个Property与默认提供的UI绑定,初始化时会查询注册表,失败则会取C盘的ProgramFile文件夹。
之后,该值会根据UI返回的情况进行变化。
<Property Id="INSTALLDIR">
<RegistrySearch Id='InstallDirRegistry' Type='raw' Root='HKLM' Key='Software\nICEnnnnnnnLee\BilibiliDown' Name='InstallDir' />
</Property>
建立Feature,在里面实现安装目录的保存
<ComponentGroup Id="MSpecificGroup">
<Component Id="cmp_RegistryEntry_InstallDir" Directory="INSTALLDIR" Guid="*">
<RegistryKey Root='HKLM' Key='Software\nICEnnnnnnnLee\BilibiliDown'>
<RegistryValue Type='string' Name='InstallDir' Value='[INSTALLDIR]' />
</RegistryKey>
</Component>
</ComponentGroup>
启用该Feature
<Feature Id="DefaultFeature" Title="!(loc.MainFeatureTitle)" Level="1">
<ComponentGroupRef Id="MSpecificGroup"/>
<ComponentGroupRef Id="Shortcuts"/>
<ComponentGroupRef Id="Files"/>
<ComponentGroupRef Id="FileAssociations"/>
</Feature>
保存下载目录
- 1 新建Property DOWNLOADDIR,默认值优先从注册表查询
<Property Id="DOWNLOADDIR" Value="C:\Users\LiJia\Downloads\"> <RegistrySearch Id='DownloadDirRegistry' Type='raw' Root='HKLM' Key='Software\nICEnnnnnnnLee\BilibiliDown' Name='DownloadDir' /> </Property>
- 2 通过Component 实现注册表位置保存
<Component Id="cmp_RegistryEntry_DownloadDir" Directory="INSTALLDIR" Guid="*"> <RegistryKey Root='HKLM' Key='Software\nICEnnnnnnnLee\BilibiliDown'> <RegistryValue Type='string' Name='DownloadDir' Value='[DOWNLOADDIR]' /> </RegistryKey> </Component>
- 3 新建UI,绑定 Property DOWNLOADDIR, 顺序在安装目录界面之前。
UI可以参考一下WiXInstallerExamples
ui.wxf
<Dialog Id="DownloadDirDlg" Width="370" Height="270" Title="[ProductName] 下载文件夹设置" NoMinimize="yes"> <!--Header--> <Control Id="BannerBitmap" Type="Bitmap" X="0" Y="0" Width="370" Height="44" TabSkip="no" Text="WixUI_Bmp_Banner" /> <Control Id="Title" Type="Text" X="15" Y="6" Width="200" Height="15" Transparent="yes" NoPrefix="yes"> <Text>{\WixUI_Font_Title}Custom download dir</Text> </Control> <Control Id="Description" Type="Text" X="25" Y="23" Width="280" Height="15" Transparent="yes" NoPrefix="yes"> <Text>请设置下载路径</Text> </Control> <Control Id="BannerLine" Type="Line" X="0" Y="44" Width="370" Height="0" /> <!--Properties--> <Control Id="NameLabel" Type="Text" X="30" Y="60" Width="100" Height="15" TabSkip="no" Text="&下载文件位置:" /> <Control Id="NameEdit" Type="Edit" X="30" Y="72" Width="220" Height="18" Property="DOWNLOADDIR" Text="{80}" /> <!--Footer--> <Control Id="BottomLine" Type="Line" X="0" Y="234" Width="370" Height="0" /> <Control Id="Back" Type="PushButton" X="180" Y="243" Width="56" Height="17" Text="&Back"> <Publish Event="NewDialog" Value="InstallDirDlg">1</Publish> </Control> <Control Id="Next" Type="PushButton" X="236" Y="243" Width="56" Height="17" Default="yes" Text="&Next"> <Publish Event="SpawnWaitDialog" Value="WaitForCostingDlg">CostingComplete = 1</Publish> <Condition Action="disable"><![CDATA[DOWNLOADDIR = ""]]></Condition> <Condition Action="enable"><![CDATA[DOWNLOADDIR <> ""]]></Condition> <Publish Event="NewDialog" Value="VerifyReadyDlg">DOWNLOADDIR</Publish> </Control> <Control Id="Cancel" Type="PushButton" X="304" Y="243" Width="56" Height="17" Cancel="yes" Text="Cancel"> <Publish Event="SpawnDialog" Value="CancelDlg">1</Publish> </Control> </Dialog>
InstallDirNotEmptyDlg.wxs
,将设置安装目录的下一个窗口改为设置下载目录<CustomAction Id="JpCheckInstallDir" BinaryKey="JpCaDll" DllEntry="CheckInstallDir" /> <SetProperty Action="SetAfterInstallDlg0" Id="AfterInstallDlg" After="JpFindRelatedProducts" Value="DownloadDirDlg">NOT Installed AND NOT REMOVEOLD</SetProperty> <SetProperty Action="SetAfterInstallDlg1" Id="AfterInstallDlg" After="JpFindRelatedProducts" Value="$(var.JpAfterInstallDirDlg)">Installed OR REMOVEOLD</SetProperty> 下一个的动作也要改改 $(var.JpAfterInstallDirDlg) -> DownloadDirDlg / [AfterInstallDlg]
- 4 新建CustomAction,将下载位置保存到 “[INSTALLDIR]/config/app.config”末尾。
可以参考一下安装时调用PowerShell的例子, 这里为了方便,直接cmd echo内容到文件末尾。
有不懂的,可以关注一下文档中的SetProperty、InstallExecuteSequence<SetProperty Id="CA_AppendDownloadDirSettings" Before ="CA_AppendDownloadDirSettings" Sequence="execute" Value='"cmd" /c call echo bilibili.savePath = [DOWNLOADDIR] >> "[INSTALLDIR]/config/app.config" ' /> <CustomAction Id="CA_AppendDownloadDirSettings" BinaryKey="WixCA" DllEntry="WixQuietExec" Execute="deferred" Return="check" Impersonate="no" /> 注册该动作 <InstallExecuteSequence> <Custom Action="CA_AppendDownloadDirSettings" Before='InstallFinalize'>NOT Installed AND NOT REMOVEOLD</Custom> </InstallExecuteSequence>