Wix 安装部署教程(四) 添加安装文件及快捷方式

时间:2022-01-23 13:19:30

通过之前三篇的介绍,大家对wix的xml部署方式也应该有一些认识,今天天气不错,再来一发。主要介绍桌面,开始菜单,卸载等功能的如何添加。希望园友们支持!

一、如何添加文件

Demo打包程序很简单,就一个exe,但实际过程中,往往还要引用一些dll,配置文件。我们如何安装到目标文件下呢。这个就比windows installer 麻烦些了,在windows installer中直接一个添加引用就可以了。 但wix也不麻烦,首先要明白各个元素的作用,Directory定义了安装目录,ComponentGroup和DirectoryRef包含的Component和File 定义文件的正真的路径。然后Feature 就是一个安装清单,告诉wix需要安装的文件是哪些。 我们试着在安装目录下增加一个文件,放入一个dll和一个xml文件。

1.先在wix工程中新建一个文件夹,把我们需要打包的文件copy进来。

2.再修改目标文件夹。在Setup07下面加入一个Id="Demo"的Directory

  <Fragment>
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLFOLDER" Name="Setup07" >
<Directory Id="Demo" Name="Demo" />
</Directory>
</Directory>
</Directory>
</Fragment>

3.定义需要添加的文件的位置。在Framment 元素块。添加一个DriectoryRef 元素,id指向Demo,

  <Fragment>
<ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
<Component Id="ProductComponent">
<File Id="myapplication.exe" Source="$(var.MyApplication.TargetPath)" />
</Component>
</ComponentGroup>
<DirectoryRef Id="Demo">
<Component Id="Variable.xml" Guid="5E254682-DD5F-453D-8333-844457282026">
<File Id="Variable.xml" Source="content/Variable.xml" />
<File Id="mydll" Source="content/CxIODrvWrapper.dll" />
</Component>
</DirectoryRef>
</Fragment>

如果想保持文件,在卸载的时候不被删除,用Permanent

   <Component Id='keepfile'  Guid='{031FD8D4-CCD9-446B-B876-F20816DFAB39}' Permanent='yes' >
<File Source='$(var.Dev)IOServer\msvcr110d.dll' Id='msvcr110d' />
</Component>

我们可以看见,对于ComponentGroup直接用的是Dirctory属性INSTALLFOLDER指向上个Fragment中的Directory。他其中的file都可以安装在Setup07文件夹下。而我们的DirectoryRef 的Id指向我们创建的Demo文件夹,这里要说明的是Component 安装组件的意思,是必须和feature元素对应起来的,且其中可以包含多个File。然后修改我们的Feature元素。添加一段。关于Guid,直接在VS -工具-创建GUID 再copy出来就行。 不用每次都那么生成,把生产的随便改几个数字也行。

  <ComponentRef Id="Variable.xml"/>

这个ComponentRef就直接对应了id为Variable.xml的Component的元素,告诉wix 需要安装一个这样的组件。 这样在生成就可以安装了。在你的C:\Program Files (x86)\Setup07\Demo 目录下就可以看见dll和xml文件了。

上面的ComponentGroup也可以改掉。 效果一样。

<Fragment>
<!--<ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
<Component Id="ProductComponent">
<File Id="myapplication.exe" Source="$(var.MyApplication.TargetPath)" />
</Component>
</ComponentGroup>-->
<DirectoryRef Id="INSTALLFOLDER">
<Component Id="myapplication.exe" Guid="5E254682-DD5F-453D-8323-844457212026">
<File Id="myapplication.exe" Source="$(var.MyApplication.TargetPath)" />
</Component>
</DirectoryRef>
<DirectoryRef Id="Demo">
<Component Id="Variable.xml" Guid="5E254682-DD5F-453D-8333-844457282026">
<File Id="Variable.xml" Source="content/Variable.xml" />
<File Id="mydll" Source="content/CxIODrvWrapper.dll" />
</Component>
</DirectoryRef>
</Fragment>
.......
<Feature Id="ProductFeature" Title="Setup07" Level="1">
<!--<ComponentGroupRef Id="ProductComponents" />-->
<ComponentRef Id="Variable.xml"/>
<ComponentRef Id="myapplication.exe"/>
</Feature>

二、增加菜单栏快捷方式

我们先在菜单栏中加入快捷方式

1.在安装目录Fragment元素中,添加一个Directory。记住这个要插入在<Directory Id="TARGETDIR" Name="SourceDir"> 下面。

<Directory Id="ProgramMenuFolder">
<Directory Id="ApplicationProgramsFolder" Name="WixSetUp"/>
</Directory>

这里的ProgramMenuFolder就是只的windows的开始菜单,第二级的directory就是要在菜单中显示的文件,名称为wixsetup。

2.接下我们再这个目录下加入快捷方式,需要用DirectoryRef元素了。Shortcut 就是生成快捷方式的元素。

 <DirectoryRef Id="ApplicationProgramsFolder">
<Component Id="ApplicationShortcut" Guid="5A254682-DD5F-453D-8333-144457282026">
<Shortcut Id="ApplicationStartMenuShortcut"
Name="Mysetup"
Description="my first application installed by wix"
Target="[INSTALLFOLDER]MyApplication.exe"
WorkingDirectory="INSTALLFOLDER"/>
<RemoveFolder Id="ApplicationProgramsFolder" On="uninstall"/>
<RegistryValue Root="HKCU" Key="Software\Microsoft\MyApplicationName" Name="installed" Type="integer" Value="1" KeyPath="yes"/>
</Component>
</DirectoryRef>

需要说明的是,Target的写法,指向我们安装目录下的exe,中括号中加入directory的Id,再紧跟应用程序的名字。 这里的RemoveFolder和Registry元素,官方文档说是为了避免出现ICE 验证错误才结合Shortcut一起出现,我也有点晕,只能先加上了。感兴趣的可以看下这里的http://msdn.microsoft.com/en-us/library/aa368942.aspx 对这种验证的解释。vs生成出错,都是ice开头的。

最后我们在feature中加入Component的列表。

 <ComponentRef Id="ApplicationShortcut" />

在编译生成,安装完成后,我们的菜单栏中就会出现WixSetup/Mysetup的目录。打开可以运行,但这个时候快捷方式的图标还没有改变,先记住这个问题,再继续往下走。

最近发现一个奇怪的问题,需要做成快捷方式的exe,他们的id必须和文件一直,否则这个快捷方式在安装之后找不到这个exe。

   <Shortcut Id="DesktopShortcut" Directory="DesktopFolder"   Name="Runtime " Target="[dirE77F08C3A4BE230B19F4D6C90A249E4F]HMIRun.exe"
WorkingDirectory="dirE77F08C3A4BE230B19F4D6C90A249E4F" Icon="Startshorcut" />
<Shortcut Id="DesktopShortcut2" Directory="DesktopFolder" Name="SACADDev" Target="[INSTALLFOLDER]SCADAFramework.exe"
WorkingDirectory="INSTALLFOLDER" Icon="devshorcut" />
....
<File Source='$(var.Dev)HMIRun\HMIRun.exe' Id='HMIRun.exe' />

而如果换成加密的那种id,却找不到。

此时的全代码如下

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="*" Name="!(loc.ApplicationName)"
Language="1033" Version="1.0.0.0" Manufacturer="RJStone" UpgradeCode="3486eddf-0957-45cf-8c26-3de8bceca2b4">
<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" /> <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
<MediaTemplate /> <!--安装列表-->
<Feature Id="ProductFeature" Title="Setup07" Level="1">
<!--<ComponentGroupRef Id="ProductComponents" />-->
<ComponentRef Id="Variable.xml"/>
<ComponentRef Id="myapplication.exe"/>
<ComponentRef Id="ApplicationShortcut" /> </Feature> <!--自定义安装界面-->
<WixVariable Id="WixUILicenseRtf" Value="lisences.rtf" />
<WixVariable Id="WixUIDialogBmp" Value="bb.jpg"/>
<WixVariable Id="WixUIBannerBmp" Value="top.jpg"/> <!--安装完成后启动程序-->
<Property Id="WIXUI_EXITDIALOGOPTIONALTEXT" Value="Thank you for installing this product." />
<Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT" Value="Launch this Application " />
<Property Id="WixShellExecTarget" Value="[#myapplication.exe]" />
<CustomAction Id="LaunchApplication" BinaryKey="WixCA" DllEntry="WixShellExec" Impersonate="yes" /> <!--UI风格-->
<UI Id="WixUI_Mondo">
<TextStyle Id="WixUI_Font_Normal" FaceName="Tahoma" Size="8" />
<TextStyle Id="WixUI_Font_Bigger" FaceName="Tahoma" Size="12" />
<TextStyle Id="WixUI_Font_Title" FaceName="Tahoma" Size="9" Bold="yes" /> <Property Id="DefaultUIFont" Value="WixUI_Font_Normal" />
<Property Id="WixUI_Mode" Value="Mondo" />
<DialogRef Id="ErrorDlg" />
<DialogRef Id="FatalError" />
<DialogRef Id="FilesInUse" />
<DialogRef Id="MsiRMFilesInUse" />
<DialogRef Id="PrepareDlg" />
<DialogRef Id="ProgressDlg" />
<DialogRef Id="ResumeDlg" />
<DialogRef Id="UserExit" /> <Publish Dialog="ExitDialog" Control="Finish" Event="EndDialog" Value="Return" Order="999">1</Publish> <Publish Dialog="WelcomeDlg" Control="Next" Event="NewDialog" Value="SpecialDlg">NOT Installed AND NOT PATCH</Publish>
<Publish Dialog="WelcomeDlg" Control="Next" Event="NewDialog" Value="VerifyReadyDlg">Installed AND PATCH</Publish> <Publish Dialog="SpecialDlg" Control="Back" Event="NewDialog" Value="WelcomeDlg">1</Publish>
<Publish Dialog="SpecialDlg" Control="Next" Event="NewDialog" Value="LicenseAgreementDlg">1</Publish> <Publish Dialog="LicenseAgreementDlg" Control="Back" Event="NewDialog" Value="SpecialDlg">1</Publish>
<Publish Dialog="LicenseAgreementDlg" Control="Next" Event="NewDialog" Value="SetupTypeDlg" Order="2">LicenseAccepted = "1"</Publish> <Publish Dialog="SetupTypeDlg" Control="Back" Event="NewDialog" Value="LicenseAgreementDlg">1</Publish>
<Publish Dialog="SetupTypeDlg" Control="TypicalButton" Event="NewDialog" Value="VerifyReadyDlg">1</Publish>
<Publish Dialog="SetupTypeDlg" Control="CustomButton" Event="NewDialog" Value="CustomizeDlg">1</Publish>
<Publish Dialog="SetupTypeDlg" Control="CompleteButton" Event="NewDialog" Value="VerifyReadyDlg">1</Publish> <Publish Dialog="CustomizeDlg" Control="Back" Event="NewDialog" Value="MaintenanceTypeDlg" Order="1">WixUI_InstallMode = "Change"</Publish>
<Publish Dialog="CustomizeDlg" Control="Back" Event="NewDialog" Value="SetupTypeDlg" Order="2">WixUI_InstallMode = "InstallCustom"</Publish>
<Publish Dialog="CustomizeDlg" Control="Next" Event="NewDialog" Value="VerifyReadyDlg">1</Publish> <Publish Dialog="VerifyReadyDlg" Control="Back" Event="NewDialog" Value="CustomizeDlg" Order="1">WixUI_InstallMode = "InstallCustom"</Publish>
<Publish Dialog="VerifyReadyDlg" Control="Back" Event="NewDialog" Value="SetupTypeDlg" Order="2">WixUI_InstallMode = "InstallTypical" OR WixUI_InstallMode = "InstallComplete"</Publish>
<Publish Dialog="VerifyReadyDlg" Control="Back" Event="NewDialog" Value="CustomizeDlg" Order="3">WixUI_InstallMode = "Change"</Publish>
<Publish Dialog="VerifyReadyDlg" Control="Back" Event="NewDialog" Value="MaintenanceTypeDlg" Order="4">WixUI_InstallMode = "Repair" OR WixUI_InstallMode = "Remove"</Publish>
<Publish Dialog="VerifyReadyDlg" Control="Back" Event="NewDialog" Value="WelcomeDlg" Order="2">WixUI_InstallMode = "Update"</Publish> <Publish Dialog="MaintenanceWelcomeDlg" Control="Next" Event="NewDialog" Value="MaintenanceTypeDlg">1</Publish> <Publish Dialog="MaintenanceTypeDlg" Control="ChangeButton" Event="NewDialog" Value="CustomizeDlg">1</Publish>
<Publish Dialog="MaintenanceTypeDlg" Control="RepairButton" Event="NewDialog" Value="VerifyReadyDlg">1</Publish>
<Publish Dialog="MaintenanceTypeDlg" Control="RemoveButton" Event="NewDialog" Value="VerifyReadyDlg">1</Publish>
<Publish Dialog="MaintenanceTypeDlg" Control="Back" Event="NewDialog" Value="MaintenanceWelcomeDlg">1</Publish> <Publish Dialog="ExitDialog"
Control="Finish"
Event="DoAction"
Value="LaunchApplication">WIXUI_EXITDIALOGOPTIONALCHECKBOX = 1 and NOT Installed</Publish>
</UI>
<UIRef Id="WixUI_Common" /> </Product> <!--目标安装目录-->
<Fragment>
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLFOLDER" Name="Setup07" >
<Directory Id="Demo" Name="Demo" />
</Directory>
</Directory> <Directory Id="ProgramMenuFolder">
<Directory Id="ApplicationProgramsFolder" Name="WixSetUp"/>
</Directory>
</Directory>
</Fragment> <!--安装文件目录-->
<Fragment>
<!--<ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
<Component Id="ProductComponent">
<File Id="myapplication.exe" Source="$(var.MyApplication.TargetPath)" />
</Component>
</ComponentGroup>-->
<DirectoryRef Id="INSTALLFOLDER">
<Component Id="myapplication.exe" Guid="5E254682-DD5F-453D-8323-844457212026">
<File Id="myapplication.exe" Source="$(var.MyApplication.TargetPath)" />
</Component>
</DirectoryRef>
<DirectoryRef Id="Demo">
<Component Id="Variable.xml" Guid="5E254682-DD5F-453D-8333-844457282026">
<File Id="Variable.xml" Source="content/Variable.xml" />
<File Id="mydll" Source="content/CxIODrvWrapper.dll" />
</Component>
</DirectoryRef> <DirectoryRef Id="ApplicationProgramsFolder">
<Component Id="ApplicationShortcut" Guid="5A254682-DD5F-453D-8333-144457282026">
<Shortcut Id="ApplicationStartMenuShortcut"
Name="Mysetup"
Description="my first application installed by wix"
Target="[INSTALLFOLDER]MyApplication.exe"
WorkingDirectory="INSTALLFOLDER"/>
<RemoveFolder Id="ApplicationProgramsFolder" On="uninstall"/>
<RegistryValue Root="HKCU" Key="Software\Microsoft\MyApplicationName" Name="installed" Type="integer" Value="1" KeyPath="yes"/>
</Component>
</DirectoryRef> </Fragment>
</Wix>

三、增加网页和卸载快捷方式

在菜单栏的基础上,再增加一个Web快捷方式,可以跳转到你的门户网站。需要3步

1.先引入WixUtilExtension.dll  这个就不多说了(之前引用过)。

2.在工程的命名空间中加入这个扩展。这个就相当于c#的using了。

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"  xmlns:util="http://schemas.microsoft.com/wix/UtilExtension" >

3.在二中的Component中加入

    <util:InternetShortcut Id="OnlineDocumentationShortcut"
Name="Online"
Target="http://yourWebsit/Home/index"/>

和之前的Shortcut在同一级。 这样就ok了。再次安装多出一个online的快捷方式,点击跳转到指定网页。

我们再增加个卸载的快捷方式,现在只需要再添加个Shortcut就行了,Id为UninstProduct

    <Shortcut Id="UninstallProduct"
Name="Uninstall"
Description="Uninstalls My Application"
Target="[SystemFolder]msiexec.exe"
Arguments="/x [ProductCode]"/>

Target指向msiexec.exe 这才是正真执行卸载的程序,[SystemFolder] 对应它的位置。Argument通过productcode告诉它哪个是要卸载的程序。/x 就是卸载命令到这里差不多就完成了。但是之前图标没有换过来。这里就给第一个Shortcut加上一个图标。 修改为

  <Shortcut Id="ApplicationStartMenuShortcut"
Name="Mysetup"
Icon="MyApplicationIcon.exe"
Description="my first application installed by wix"
Target="[INSTALLFOLDER]MyApplication.exe"
WorkingDirectory="INSTALLFOLDER">
<Icon Id="MyApplicationIcon.exe" SourceFile="content/MyApplicationIcon.exe.ico"/>
</Shortcut>

就在这个内部加入了一个Icon属性和元素。Id命名的时候建议加上一个Icon,然后ico的图标制作,可以找在线的。这一块的代码如下

<DirectoryRef Id="ApplicationProgramsFolder">
<Component Id="ApplicationShortcut" Guid="5A254682-DD5F-453D-8333-144457282026">
<Shortcut Id="ApplicationStartMenuShortcut"
Name="Mysetup"
Icon="MyApplicationIcon.exe"
Description="my first application installed by wix"
Target="[INSTALLFOLDER]MyApplication.exe"
WorkingDirectory="INSTALLFOLDER">
<Icon Id="MyApplicationIcon.exe" SourceFile="content/MyApplicationIcon.exe.ico"/>
</Shortcut>
<util:InternetShortcut Id="OnlineDocumentationShortcut"
Name="ScadaOnline"
Target="http://yourwebsit/Home/index"/>
<Shortcut Id="UninstallProduct"
Name="Uninstall"
Description="Uninstalls My Application"
Target="[SystemFolder]msiexec.exe"
Arguments="/x [ProductCode]" />
<RemoveFolder Id="ApplicationProgramsFolder" On="uninstall"/>
<RegistryValue Root="HKCU" Key="Software\Microsoft\MyApplicationName" Name="installed" Type="integer" Value="1" KeyPath="yes"/>
</Component>
</DirectoryRef>

效果图如下:开始菜单栏中有三个图标,第一个是我们的应用程序,这个ico 看起有点模糊了。在线和卸载的图标是默认的,没有修改

Wix  安装部署教程(四) 添加安装文件及快捷方式

四、增加桌面快捷方式

现在我们还差一个桌面的快捷方式。经过几番摸索,还是整出来了。

1.需要先增加一个文件目录 ,这就代表了再桌面。

    <Directory Id="DesktopFolder" Name="Desktop" />

2.再加入Shortcut,这个shortcut就不能在之前的那个地方写了,因为那是菜单栏的。 必须用DirectoryRef ,但icon还是可以用同样一个

<DirectoryRef Id="DesktopFolder">
<Component Id="DesktopFolderShortcut" Guid="5A254682-DD1F-453D-8333-144457282026">
<Shortcut Id="DesktopShortcut" Directory="DesktopFolder" Name="myApplication" Target="[INSTALLFOLDER]MyApplication.exe" WorkingDirectory="INSTALLFOLDER" Icon="MyIcon.exe">
</Shortcut>
<RegistryValue Root="HKCU" Key="Software\Microsoft\MyApplicationName" Name="installed" Type="integer" Value="1" KeyPath="yes"/>
</Component>
</DirectoryRef>

注意,下面那个RegistryValue不加不行,果然ICEXX.... 报错。需要注册一下。 这样,再编译安装,桌面出现了快捷方式,程序运行时,在windows窗口下方的图标也变了,不再是默认的windo程序的图标了。

这一节就到这了,明天继续努力。还是那句老话:周六加班不容易,看过路过,有帮助的就顶一个。 :)