一、 環境配置
1. 安裝VS2012
2. 安裝WixToolSet。安裝完成後,VS中會多增加Windows Installer XML的項目。
二、 建構打包項目
1. 建立項目
打開VS,建立Windows Installer XML項目。
2. 編寫Produc.wxs檔案
打開Product.wxs檔案,其代碼介紹參見圖1,具體代碼請參見附錄。
圖1.
3. 設定全局變量
注意:
l Define ‘Debug’ preprocessor variable一定要勾選上。
l 全局變量在product.wix檔案中的調用為$(var.xxx)格式,其中xxx為Define preprocessor variable中的名字,如PRODUCT_NAME。
l 該product.wix所用到的全局變量有如下這些:PRODUCT_NAME=TestApp;MANUFACTURER=ABC;SOURCE_FILE_DIR=../../Release;APPLICATION_NAME=TestApp.exe;ICON_NAME=App.ico
4. 相關DLL的引用
(1). .NET FRAMEWORK校驗需要引用WixNetFxExtension.dll,該DLL的路徑在C:\Program Files (x86)\WiX Toolset v3.7\bin\WixNetFxExtension.dll,具體的路徑與實際的安裝環境有關。如果未引用編譯将會出錯。
(2). 安裝界面需要引用WixUIExtension.dll,該DLL的路徑在C:\Program Files (x86)\WiX Toolset v3.7\bin\
WixUIExtension.dll,具體的路徑與實際的安裝環境有關。如果未引用編譯将會出錯。
5. 生成MSI安裝包
編譯項目,生成MSI安裝包。
附Product.wxs代碼
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="{B7AF3561-68CB-4FEF-8DE4-C011AA462F62}" Name="$(var.PRODUCT_NAME)" Language="1033" Version="1.1.1.1"
Manufacturer="$(var.MANUFACTURER)" UpgradeCode="{780EEB5A-ED25-4E85-AF66-C9EE996B2948}">
<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
<!--net2.0 NETFRAMEWORK20;net3.0 NETFRAMEWORK30 net3.5 NETFRAMEWORK35 net4.0 NETFRAMEWORK40FULL net4.5 NETFRAMEWORK45-->
<PropertyRef Id="NETFRAMEWORK40FULL"></PropertyRef>
<Condition Message="This application requires .NET Framework 4.0.
Please install the .NET Framework then run this installer again.">
<![CDATA[Installed OR NETFRAMEWORK40FULL]]>
</Condition>
<MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
<MediaTemplate EmbedCab="yes"/>
<Feature Id="ProductFeature" Title="RandomCodePrintSetup" Level="1">
<ComponentGroupRef Id="ProductComponents" />
</Feature>
<Property Id="WIXUI_INSTALLDIR" Value="INSTALL_FOLDER"/>
<UIRef Id="WixUI_InstallDir"/>
<Icon Id="$(var.ICON_NAME)" SourceFile="$(var.SOURCE_FILE_DIR)/$(var.ICON_NAME)" />
</Product>
<Fragment>
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALL_MANUFACTURER" Name="$(var.MANUFACTURER)" >
<Directory Id="INSTALL_FOLDER" Name="$(var.PRODUCT_NAME)" />
</Directory>
</Directory>
<Directory Id="ProgramMenuFolder">
<Directory Id="ProgramMenuDir" Name="$(var.PRODUCT_NAME)"></Directory>
</Directory>
<Directory Id="DesktopFolder" Name="Desktop" />
</Directory>
</Fragment>
<Fragment>
<ComponentGroup Id="ProductComponents" Directory="INSTALL_FOLDER">
<Component Guid="{FDF4B1C0-E145-4B5B-BD80-B22958C84D6B}" Id="MainApplication">
<File Id="MainApplication" Source="$(var.SOURCE_FILE_DIR)\$(var.APPLICATION_NAME)" DiskId='1' KeyPath='yes'>
</File>
</Component>
<Component Guid="{3E004D4A-7F18-4680-97E8-5B38860208AC}" Id="OtherFile">
<File Id="Config" Source="$(var.SOURCE_FILE_DIR)\Config.dll"></File>
<File Id="TraceLog" Source="$(var.SOURCE_FILE_DIR)\ TraceLog.log"></File>
</Component>
<Component Id="ApplicationShortcut" Guid="{AEFEB53D-37C4-47CD-B7F5-0BF20C08F7B0}">
<Shortcut Id="StartMenuShortcut"
Name="$(var.PRODUCT_NAME)"
Directory="ProgramMenuDir"
Target="[INSTALL_FOLDER]$(var.APPLICATION_NAME)"
WorkingDirectory="INSTALL_FOLDER"
Icon="$(var.ICON_NAME)" IconIndex="0"
/>
<Shortcut Id="DesktopShortcut"
Name="$(var.PRODUCT_NAME)"
Directory="DesktopFolder"
Target="[INSTALL_FOLDER]$(var.APPLICATION_NAME)"
WorkingDirectory="INSTALL_FOLDER"
Icon="$(var.ICON_NAME)" IconIndex="0"
/>
<Shortcut Id="UninstallStartMenu"
Name="Uninstall"
Directory="ProgramMenuDir"
Target="[SystemFolder]msiexec.exe"
Arguments="/x [ProductCode]"
Description="Uninstall"/>
<Shortcut Id="UninstallINSTALL_FOLDER"
Name="Uninstall"
Directory="INSTALL_FOLDER"
Target="[SystemFolder]msiexec.exe"
Arguments="/x [ProductCode]"
Description="Uninstall"/>
<RemoveFolder Id="RemoveShortcutFolder" Directory="ProgramMenuDir" On="uninstall" />
<RegistryKey Root="HKCU" Key="Software\Microsoft\[Manufacturer]\[ProductName]">
<RegistryValue Name="StartMenuShortcut" Type="integer" Value="1" KeyPath="yes" />
<RegistryValue Name='Uninstall' Type='string' Value='Uninstall'/>
</RegistryKey>
</Component>
<Component Id='RemoveFiles' Guid='{F341A78A-44EA-40B7-BDFD-5CA09DF7EB3F}' KeyPath='yes'>
<RemoveFolder Id="INSTALL_FOLDER" Directory="ProgramMenuFolder" On="uninstall" />
<RemoveFolder Id="INSTALL_MANUFACTURER" Directory="ProgramMenuFolder" On="uninstall" />
<RemoveFolder Id="ProgramMenuDir" Directory="ProgramMenuFolder" On="uninstall" />
</Component>
</ComponentGroup>
</Fragment>
</Wix>