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


      通過之前三篇的介紹,大家對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>
View Code

效果圖如下:開始菜單欄中有三個圖標,第一個是我們的應用程序,這個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程序的圖標了。

  這一節就到這了,明天繼續努力。還是那句老話:周六加班不容易,看過路過,有幫助的就頂一個。 :)

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM