通過之前三篇的介紹,大家對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>
View Code
二、增加菜單欄快捷方式
我們先在菜單欄中加入快捷方式
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>
View Code
三、增加網頁和解除安裝快捷方式
在菜單欄的基礎上,再增加一個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 看起有點模糊了。線上和解除安裝的圖示是預設的,沒有修改
四、增加桌面快捷方式
現在我們還差一個桌面的快捷方式。經過幾番摸索,還是整出來了。
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程式的圖示了。
這一節就到這了,明天繼續努力。還是那句老話:周六加班不容易,看過路過,有幫助的就頂一個。 :)