因為項目需要,最近在研究Wix打包部署,園子里也有一些關於wix的博客,方方面面,講的點各不同。我自己也在測試過程中,寫下過程,以供參考。最新版本WiX Toolset v3.7,如何安裝的就不說了,可以參考 http://blog.csdn.net/rryqsh/article/details/8274832
打包關心的問題有 1).Net版本檢查 2)桌面和菜單欄的快捷方式 3)更換圖標畫面,進度條 4)自動打包 5)自動更新版本。6)安裝目錄和細節 帶着問題去探索,歡迎補充。先走通一個簡單的Demo,再回頭看概念。
第一個Wix set Up
1.先創建一個簡單的Winform程序

2. 在解決方案中右鍵添加新項目,選擇Windows Installer XML(前提你要安裝了wix Toolset v3.7),再選擇Setup Project,修改項目名稱,確定。

這樣子,解決方案下面會多一個Setup07的工程,這個工程下面有一個Product.wxs 文件,默認已經打開,這是一個XML文件,也就是工程的配置文件,初次看上去各種元素不知所雲,先不管,先需要修改三個地方,首先需要引入MyApplication,

在第三行有一個Product元素,需要在Manfacturer中加入你或者你公司的名字。
<Product Id="*" Name="Setup07" Language="1033" Version="1.0.0.0" Manufacturer="RJStone" UpgradeCode="3486eddf-0957-45cf-8c26-3de8bceca2b4">
再在最下方有一個Fragment元素塊,修改為
添加了這么一段
<Component Id="ProductComponent"> <File Source="$(var.MyApplication.TargetPath)" /> </Component>
完成之后,生成一下,這樣第一個簡單的wix打包文件已經在SetUp07的bin/debug下面生成了。

點擊Setup07.msi 就可以安裝了。 但這個時候的安裝很快,因為比較簡單不會有什么進度條,上一步下一步的,安裝完成之后桌面和菜單欄中都沒有圖標,因為你還沒有配置讓它有。但在c盤下的Rrogram Files(X86)下面可以找到Setup07這個文件夾,打開他里面就是Myapplication的exe。可以執行。
還有一個問題就是,只要改變一次這個Myapplication ,重新生成,那么打包文件也會自動生成一個新的,如果你沒有改變,只是重復點生成,打包文件是不會更新的。這其實就是借助了MSBuild的機制,自動生成打包文件。當然這個程序的卸載目前就去控制面板中手動卸載吧。
初步認識Wix
1)MSBuild 是 Microsoft Build Engine 的縮寫,代表 Microsoft 和 Visual Studio 的新的生成平台,引入了一種基於xml的文件格式。他負責生成exe文件,需要生成打包文件需要wix, MSBuild 園子知識庫的簡解 http://kb.cnblogs.com/page/45681/
wix官方文檔說:用Msbulid生成wix,需要創建一個.wixproj 的文件,其實這個文件上面的Setup07中VS已經幫我們自動生成,只是要打開文件夾才能看到。
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">x86</Platform> <ProductVersion>3.7</ProductVersion> <ProjectGuid>384a3edb-dbda-4948-9071-ae6a05968f55</ProjectGuid> <SchemaVersion>2.0</SchemaVersion> <OutputName>Setup07</OutputName> <OutputType>Package</OutputType> <WixTargetsPath Condition=" '$(WixTargetsPath)' == '' AND '$(MSBuildExtensionsPath32)' != '' ">$(MSBuildExtensionsPath32)\Microsoft\WiX\v3.x\Wix.targets</WixTargetsPath> <WixTargetsPath Condition=" '$(WixTargetsPath)' == '' ">$(MSBuildExtensionsPath)\Microsoft\WiX\v3.x\Wix.targets</WixTargetsPath> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> <OutputPath>bin\$(Configuration)\</OutputPath> <IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath> <DefineConstants>Debug</DefineConstants> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> <OutputPath>bin\$(Configuration)\</OutputPath> <IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath> </PropertyGroup> <ItemGroup> <Compile Include="Product.wxs" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\MyApplication\MyApplication.csproj"> <Name>MyApplication</Name> <Project>{b16ec091-efc7-4cd3-b62b-eec13303d72a}</Project> <Private>True</Private> <DoNotHarvest>True</DoNotHarvest> <RefProjectOutputGroups>Binaries;Content;Satellites</RefProjectOutputGroups> <RefTargetDir>INSTALLFOLDER</RefTargetDir> </ProjectReference> </ItemGroup> <Import Project="$(WixTargetsPath)" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Wix.targets. <Target Name="BeforeBuild"> </Target> <Target Name="AfterBuild"> </Target> --> </Project>
2)XML文檔元素
Wix本身就是基於XML,因此定義了很多特定的元素,剛開始一看,因為不熟悉容易搞暈。 先也只是初步認識,深入了解還是需要后面的不斷測試。不全面的可以去看官方的文檔。
* Product 的父級只有wix,主要由Id,Language,等屬性
Id:產品的GUID,GUID可以借助VS-->工具--->創建GUID 會有五種格式來供選擇,這里用4,5,6。不要括號。
Codepage:指所在地區的代碼頁,用來進行區域區分
Language:指所在地區使用的語言,為數字編號,
Manufacturer:制作廠商
Name:產品名稱
UpgradeCode:產品更新的GUID
Version:版本號
下來列舉幾個常見的區域代號:
語言 語言-國家 Language Codepage
English en-us 1033 1252
Simplified Chinese zh-cn 2052 936
Traditional Chinese zh-tw 1028 950
*Package 記錄一些安裝包的信息
InstallerVersion:安裝此安裝包需要的最小Windows Installer版本,用Windows Installer的主要版本乘以100 加上Window Installer的次要版本。 比如 “200” 代表的是Windows Installer2.0,而405代表的是Windows Installer4.5.
Compressed: 這個為Yes 表示在源文件中含有壓縮文件,對於Merge Module這個屬性不必設置。反正為 NoType.
InstallPrivileges:字面意思為安裝優先級,有limited 和 elevated兩種 后者是默認值,
InstallScope: 值為枚舉類型,字面意思為安裝范圍,值必須是perMachine 或者 perUser. 看文檔不知道確切的用處,先放過。
*MajorUpgrade 這個元素支持防止降級
AllowDowngrades:即回到低版本,如果設置為No(默認值) 會被阻止,這個時候DowngradeErrorMessage 屬性必須設置,以給出提示。
DowngradeErrorMessage:當你安裝一個低版本的安裝包時會給出的提示。
*Feature 一個特性表,特性是可安裝的最小單元。 子元素中的ComponentGroupRef 是和 ComponentGroup對應的。前者相當於一個安裝目錄,后者記錄了安裝文件的具體位置。
Id:是唯一的
Title:就是個短的說明。
Level:安裝的等級,值為0 會使這個特性無效,默認值為1
Absent:這個屬性定義User是否有權在用戶接口中去選擇使某個特性不安裝(absent),值為allow或者disallow之一
*Fragment 元素是在wix中創建一個安裝數據庫的基礎塊(msi文件就是個數據庫),定義之后,是不可改變的(目前不太理解....ORZ)。它的子元素中含有*Ref的元素必須有對應的單元,比如在Fragment中含有兩個Component元素,那么 你必須在Feature中用ComponentRef 與Component對應. 默認生成的文檔中含有兩個Fragment塊。 一個包含的是Directory,一個包含的是ComponentGroup ,前者指的是安裝目錄,后者顧名思義就是Component的一個集合。
<Fragment> <Directory Id="TARGETDIR" Name="SourceDir"> <Directory Id="ProgramFilesFolder"> <Directory Id="INSTALLFOLDER" Name="Setup07" /> </Directory> </Directory> </Fragment> <Fragment> <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER"> <!-- TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. --> <!-- <Component Id="ProductComponent"> --> <!-- TODO: Insert files, registry keys, and other resources here. --> <Component Id="ProductComponent"> <File Source="$(var.MyApplication.TargetPath)" /> </Component> </ComponentGroup> </Fragment>
*Directory 產品的安裝目錄。第一個Directory Id=TARGETDIR 是根目錄,每一個wix工程都會有這個目錄元素,第二個Id=ProgramFilesFolder 對應的就是C:\Program Files\ ,64位的就在x86下,第三個元素就是我們自己的應用程序所在的文件夾,最終形成就是c:\Program Files\My Application Name 在默認的提示中 也可以看見在Component 中添加安裝文件,資源,注冊表等。這里可以看見最里面的Directory的Id=INSTALLFOLDER是和第二個Fragment中的ComponentGroup的Directory屬性是一致的。在Component中就是每一個你需要安裝的單元以及它的位置。
$(var.MyApplication.TargetPath)是wix引用變量。見下表。當然vs2010只是當前版本的意思。
| Variable name |
Example usage |
Example value |
| var.ProjectName.Configuration |
$(var.MyProject.Configuration) |
Debug or Release |
| var.ProjectName.FullConfiguration |
$(var.MyProject.FullConfiguration) |
Debug|AnyCPU |
| var.ProjectName.Platform |
$(var.MyProject.Platform) |
AnyCPU, Win32, x64 or ia64 |
| var.ProjectName.ProjectDir |
$(var.MyProject.ProjectDir) |
C:\users\myusername\Documents\Visual Studio 2010\Projects\MyProject\ |
| var.ProjectName.ProjectExt |
$(var.MyProject.ProjectExt) |
.csproj |
| var.ProjectName.ProjectFileName |
$(var.MyProject.ProjectFileName) |
MyProject.csproj |
| var.ProjectName.ProjectName |
$(var.MyProject.ProjectName) |
MyProject |
| var.ProjectName.ProjectPath |
$(var.MyProject.ProjectPath) |
C:\users\myusername\Documents\Visual Studio 2010\Projects\MyProject\MyApp.csproj |
| var.ProjectName.TargetDir |
$(var.MyProject.TargetDir) |
C:\users\myusername\Documents\Visual Studio 2010\Projects\MyProject\bin\Debug\ |
| var.ProjectName.TargetExt |
$(var.MyProject.TargetExt) |
.exe |
| var.ProjectName.TargetFileName |
$(var.MyProject.TargetFileName) |
MyProject.exe |
| var.ProjectName.TargetName |
$(var.MyProject.TargetName) |
MyProject |
| var.ProjectName.TargetPath |
$(var.MyProject.TargetPath) |
C:\users\myusername\Documents\Visual Studio 2010\Projects\MyProject\bin\Debug\MyProject.exe |
| var.ProjectName.Culture.TargetPath |
$(var.MyProject.en-US.TargetPath) |
C:\users\myusername\Documents\Visual Studio 2010\Projects\MyProject\bin\Debug\en-US\MyProject.msm |
| var.SolutionDir |
$(var.SolutionDir) |
C:\users\myusername\Documents\Visual Studio 2010\Projects\MySolution\ |
| var.SolutionExt |
$(var.SolutionExt) |
.sln |
| var.SolutionFileName |
$(var.SolutionFileName) |
MySolution.sln |
| var.SolutionName |
$(var.SolutionName) |
MySolution |
| var.SolutionPath |
$(var.SolutionPath) |
C:\users\myusername\Documents\Visual Studio 2010\Projects\MySolution\MySolution.sln |
整體看一下 Directory定義了安裝目錄,Component定義了組件及位置,Feature定義了組件在安裝時的一些特性。Product和package等定義了安裝包的信息。所以還是比較語義化的,繼續探索。
以上只是個人小結,難免有所不當,歡迎指正,如果對你有幫助,請幫忙頂一下。:)
