Create Shortcut/Registry in an Installer
昨天添加完UI后,我们Installer终于可以和用户交互了,虽然很简单,但总算有个好的开头...可是安装完之后呢?额...我们的产品到哪里启动啊?!太大意了,居然没有给程序做快捷方式!总不能每次让使用者从C:\Program Files\WixProject\下启动吧~
那在WiX中怎么创建呢?
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="e6ecf2a7-b03c-4b3f-ace8-6179ab62e4c7" Name="WixProject" Language="1033" Version="1.0.0.0" Manufacturer="WixProject" UpgradeCode="b3d503b8-4f21-4116-8562-6ff1e0e16a28">
<Package InstallerVersion="200" Compressed="yes" />
<Media Id="1" Cabinet="media1.cab" EmbedCab="yes" />
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLLOCATION" Name="WixProject">
<Component Id="Com.ProductComponent" Guid="266b1212-45bf-4b65-a31a-29c3bdc43e20">
<File Id="WindowsFormsApplication.exe" Source="$(var.SolutionDir)\WindowsFormsApplication\$(var.OutDir)\WindowsFormsApplication.exe" KeyPath="yes">
<Shortcut Id="WindowsFormsApplicationStartmenu" Directory="ProgramMenuDir" Name="Windows Forms Application"
WorkingDirectory='INSTALLDIR' Icon="WindowsFormsApplicationIcon.exe" IconIndex="0" Advertise="yes" />
<Shortcut Id="WindowsFormsApplicationIconDesktop" Directory="DesktopFolder" Name="Windows Forms Application"
WorkingDirectory='INSTALLDIR' Icon="WindowsFormsApplicationIcon.exe" IconIndex="0" Advertise="yes" />
</File>
</Component>
</Directory>
</Directory>
<Directory Id="ProgramMenuFolder" Name="Programs">
<Directory Id="ProgramMenuDir" Name="Windows Forms Application">
<Component Id="Com.ProgramMenuDir" Guid="0194D405-F08A-4a86-AA83-75E9EB336BBE">
<RemoveFolder Id='ProgramMenuDir' On='uninstall' />
<RegistryValue Id='regValue' Root='HKCU' Type='string' Key='Software\[Manufacturer]\[ProductName]' Name='Location' Value='[INSTALLLOCATION]'/>
</Component>
</Directory>
</Directory>
<Component Id="Com.Reg" Guid="E5619341-E3B8-4513-970B-A9924DE3EE26">
<RegistryKey Root="HKLM" Key="Software\[Manufacturer]\[ProductName]" Action="createAndRemoveOnUninstall">
<RegistryValue Type="string" Name="Product" Value="[ProductName]" KeyPath="yes" />
<RegistryValue Type="string" Name="Version" Value="[ProductVersion]"/>
<RegistryValue Type="string" Name="Location" Value="[INSTALLLOCATION]"/>
<RegistryValue Type="string" Name="Language" Value="[ProductLanguage]"/>
</RegistryKey>
</Component>
<Directory Id="DesktopFolder" Name="Desktop" />
</Directory>
<Feature Id="Product" Title="Windows Form sApplication" Level="1">
<ComponentRef Id="Com.ProductComponent" />
</Feature>
<Feature Id="Others" Title="Shortcuts,Registry" Level="1">
<ComponentRef Id="Com.ProgramMenuDir" />
<ComponentRef Id="Com.Reg" />
</Feature>
<Icon Id="WindowsFormsApplicationIcon.exe" SourceFile="$(var.SolutionDir)\WindowsFormsApplication\$(var.OutDir)\WindowsFormsApplication.exe" />
<UIRef Id="WixUI_FeatureTree"/>
</Product>
</Wix>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="e6ecf2a7-b03c-4b3f-ace8-6179ab62e4c7" Name="WixProject" Language="1033" Version="1.0.0.0" Manufacturer="WixProject" UpgradeCode="b3d503b8-4f21-4116-8562-6ff1e0e16a28">
<Package InstallerVersion="200" Compressed="yes" />
<Media Id="1" Cabinet="media1.cab" EmbedCab="yes" />
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLLOCATION" Name="WixProject">
<Component Id="Com.ProductComponent" Guid="266b1212-45bf-4b65-a31a-29c3bdc43e20">
<File Id="WindowsFormsApplication.exe" Source="$(var.SolutionDir)\WindowsFormsApplication\$(var.OutDir)\WindowsFormsApplication.exe" KeyPath="yes">
<Shortcut Id="WindowsFormsApplicationStartmenu" Directory="ProgramMenuDir" Name="Windows Forms Application"
WorkingDirectory='INSTALLDIR' Icon="WindowsFormsApplicationIcon.exe" IconIndex="0" Advertise="yes" />
<Shortcut Id="WindowsFormsApplicationIconDesktop" Directory="DesktopFolder" Name="Windows Forms Application"
WorkingDirectory='INSTALLDIR' Icon="WindowsFormsApplicationIcon.exe" IconIndex="0" Advertise="yes" />
</File>
</Component>
</Directory>
</Directory>
<Directory Id="ProgramMenuFolder" Name="Programs">
<Directory Id="ProgramMenuDir" Name="Windows Forms Application">
<Component Id="Com.ProgramMenuDir" Guid="0194D405-F08A-4a86-AA83-75E9EB336BBE">
<RemoveFolder Id='ProgramMenuDir' On='uninstall' />
<RegistryValue Id='regValue' Root='HKCU' Type='string' Key='Software\[Manufacturer]\[ProductName]' Name='Location' Value='[INSTALLLOCATION]'/>
</Component>
</Directory>
</Directory>
<Component Id="Com.Reg" Guid="E5619341-E3B8-4513-970B-A9924DE3EE26">
<RegistryKey Root="HKLM" Key="Software\[Manufacturer]\[ProductName]" Action="createAndRemoveOnUninstall">
<RegistryValue Type="string" Name="Product" Value="[ProductName]" KeyPath="yes" />
<RegistryValue Type="string" Name="Version" Value="[ProductVersion]"/>
<RegistryValue Type="string" Name="Location" Value="[INSTALLLOCATION]"/>
<RegistryValue Type="string" Name="Language" Value="[ProductLanguage]"/>
</RegistryKey>
</Component>
<Directory Id="DesktopFolder" Name="Desktop" />
</Directory>
<Feature Id="Product" Title="Windows Form sApplication" Level="1">
<ComponentRef Id="Com.ProductComponent" />
</Feature>
<Feature Id="Others" Title="Shortcuts,Registry" Level="1">
<ComponentRef Id="Com.ProgramMenuDir" />
<ComponentRef Id="Com.Reg" />
</Feature>
<Icon Id="WindowsFormsApplicationIcon.exe" SourceFile="$(var.SolutionDir)\WindowsFormsApplication\$(var.OutDir)\WindowsFormsApplication.exe" />
<UIRef Id="WixUI_FeatureTree"/>
</Product>
</Wix>
有点儿多且乱哈,没关系,我们一步一步来看~先看快捷方式.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<Shortcut Id="WindowsFormsApplicationStartmenu" Directory="ProgramMenuDir" Name="Windows Forms Application"
WorkingDirectory='INSTALLDIR' Icon="WindowsFormsApplicationIcon.exe" IconIndex="0" Advertise="yes" />
<Shortcut Id="WindowsFormsApplicationIconDesktop" Directory="DesktopFolder" Name="Windows Forms Application"
WorkingDirectory='INSTALLDIR' Icon="WindowsFormsApplicationIcon.exe" IconIndex="0" Advertise="yes" />
WorkingDirectory='INSTALLDIR' Icon="WindowsFormsApplicationIcon.exe" IconIndex="0" Advertise="yes" />
<Shortcut Id="WindowsFormsApplicationIconDesktop" Directory="DesktopFolder" Name="Windows Forms Application"
WorkingDirectory='INSTALLDIR' Icon="WindowsFormsApplicationIcon.exe" IconIndex="0" Advertise="yes" />
除了快捷方式,将产品信息写入注册表也是必须做的(主要是给其他程序查询自己提供方便,当然卸载的时候一定记着清理掉...不然就"流氓"了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<Component Id="Com.ProgramMenuDir" Guid="0194D405-F08A-4a86-AA83-75E9EB336BBE">
<RemoveFolder Id='ProgramMenuDir' On='uninstall' />
<RegistryValue Id='regValue' Root='HKCU' Type='string' Key='Software\[Manufacturer]\[ProductName]' Name='Location' Value='[INSTALLLOCATION]'/>
</Component>
......
<Component Id="Com.Reg" Guid="E5619341-E3B8-4513-970B-A9924DE3EE26">
<RegistryKey Root="HKLM" Key="Software\[Manufacturer]\[ProductName]" Action="createAndRemoveOnUninstall">
<RegistryValue Type="string" Name="Product" Value="[ProductName]" KeyPath="yes" />
<RegistryValue Type="string" Name="Version" Value="1.0.0.0"/>
<RegistryValue Type="string" Name="Location" Value="[INSTALLLOCATION]"/>
<RegistryValue Type="string" Name="Language" Value="[ProductLanguage]"/>
</RegistryKey>
</Component>
<RemoveFolder Id='ProgramMenuDir' On='uninstall' />
<RegistryValue Id='regValue' Root='HKCU' Type='string' Key='Software\[Manufacturer]\[ProductName]' Name='Location' Value='[INSTALLLOCATION]'/>
</Component>
......
<Component Id="Com.Reg" Guid="E5619341-E3B8-4513-970B-A9924DE3EE26">
<RegistryKey Root="HKLM" Key="Software\[Manufacturer]\[ProductName]" Action="createAndRemoveOnUninstall">
<RegistryValue Type="string" Name="Product" Value="[ProductName]" KeyPath="yes" />
<RegistryValue Type="string" Name="Version" Value="1.0.0.0"/>
<RegistryValue Type="string" Name="Location" Value="[INSTALLLOCATION]"/>
<RegistryValue Type="string" Name="Language" Value="[ProductLanguage]"/>
</RegistryKey>
</Component>
对了,我们还增加了一个Feature项,结合"WixUI_FeatureTree",看看有什么不一样的地方?
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<Feature Id="Product" Title="Windows Form sApplication" Level="1">
<ComponentRef Id="Com.ProductComponent" />
</Feature>
<Feature Id="Others" Title="Shortcuts,Registry" Level="1">
<ComponentRef Id="Com.ProgramMenuDir" />
<ComponentRef Id="Com.Reg" />
</Feature>
......
<UIRef Id="WixUI_FeatureTree"/>
<ComponentRef Id="Com.ProductComponent" />
</Feature>
<Feature Id="Others" Title="Shortcuts,Registry" Level="1">
<ComponentRef Id="Com.ProgramMenuDir" />
<ComponentRef Id="Com.Reg" />
</Feature>
......
<UIRef Id="WixUI_FeatureTree"/>
好了,这下用户可以很方便的启动我们的程序了!
开始菜单
桌面
不知道有人试过没,把"Compressed"或者"EmbedCab"设置成"no",编译后有啥效果?
Compressed="no",需要安装的文件将不会被打包在MSI里,而是放在WixProject目录中(可以随便替换被安装文件~)
EmbedCab="no"