I'm using WixSharp to put together an installer. I'd like to have a shortcut in the Program Files\ menu to open a开发者_如何学编程 webpage. Can I do that with WixSharp?
In WixSharp you can create an InternetShortcut via the InternetShortcut class.
Below is an example from an app I'm working on of adding a link to a website that includes an icon via the InternetShortcut
class and placing that link on both the Desktop and Start Menu.
var project = new Project("MyApplicationName", // Installer name
new Dir(@"%ProgramFiles%\MyApplicationName", // Install directory
new Files(@"..\MyApplicationName\bin\Release\netcoreapp3.1\publish\*.*")), // Source directory
new Dir(@"%ProgramMenu%\MyApplicationName",
new InternetShortcut
{
Name = $"Admin Page",
Target = "http://localhost:4444",
Type = InternetShortcut.ShortcutType.link,
AttributesDefinition = @"IconFile=[INSTALLDIR]\icon.ico;IconIndex=0"
},
new ExeFileShortcut
{
Name = "Uninstall",
Target = "[System64Folder]msiexec.exe",
Arguments = "/x [ProductCode]"
}),
new Dir(@"%Desktop%",
new InternetShortcut
{
Name = $"Admin Page",
Target = "http://localhost:4444",
Type = InternetShortcut.ShortcutType.link,
AttributesDefinition = @"IconFile=[INSTALLDIR]\icon.ico;IconIndex=0"
})
);
project.InstallScope = InstallScope.perMachine;
See also:
- Shortcut for Website #84
- InternetShortcut Element (Util Extension)
Use the Wix# XML injection feature to place the WiX code for an Internet shortcut into your build. Using this WiX syntax example for an Internet shortcut:
<util:InternetShortcut Id="OnlineDocumentationShortcut"
Name="My Online Documentation"
Target="http://wixtoolset.org/"/>
In your Wix# installer code, first, in your main code, you would add a handler to the "WixSourceGenerated" event, which fires after the .wxs file has been created, but before it is compiled. That code would look like this:
// Hook up a delegate to the "WixSourceGenerated" event, fires when .wxs file is fully created
Compiler.WixSourceGenerated += InjectXMLElement;
// Make sure the .wxs file gets preserved
Compiler.PreserveTempFiles = true;
// Trigger the MSI file build
Compiler.BuildMsi(project);
Then in your delegate method, you would have code that looks something like this:
/// <summary>
/// Insert XML elements and attributes into the generated .wxs file
/// </summary>
/// <param name="document"></param>
static void InjectXMLElement(System.Xml.Linq.XDocument document)
{
// To add an Internet shortcut on target system, add this element:
// <util:InternetShortcut Id="OnlineDocumentationShortcut"
// Name="My Online Documentation"
// Target="http://wixtoolset.org/"/>
var componentElement = document.Root.Select("Product/Directory/Directory/Component");
componentElement.Add(new XElement("util:InternetShortcut",
new XAttribute("Id", "OnlineDocumentationShortcut"),
new XAttribute("Target", "http://wixtoolset.org/")));
}
You will need to look in your generated .wxs file, which will be in the same folder as your generated MSI file, and figure out what the XPath is, for "document.Root.Select()" to get to the node where you want to add the inserted WiX XML. In my wxs file, the Start Menu shortcuts are in a section of the XML that looks like this:
<Directory Id="ProgramMenuFolder" Name="ProgramMenuFolder">
<Directory Id="ProgramMenuFolder.My_App_Name" Name="My App Name">
<Component Id="My_App_Name.EmptyDirectory" Guid="18342da3-5a42-4397-b522-5927ace999">
<CreateFolder />
<RemoveFolder Id="ProgramMenuFolder.My_App_Name" On="uninstall" />
<RegistryKey Root="HKCU" Key="Software\WixSharp\Used">
<RegistryValue Value="0" Type="string" KeyPath="yes" />
</RegistryKey>
</Component>
</Directory>
So to add an Internet shortcut there, you would want the resulting XML to look something like this:
<Directory Id="ProgramMenuFolder" Name="ProgramMenuFolder">
<Directory Id="ProgramMenuFolder.My_App_Name" Name="My App Name">
<Component Id="My_App_Name.EmptyDirectory" Guid="18342da3-5a42-4397-b522-5927ace999">
<CreateFolder />
<util:InternetShortcut Id="OnlineDocumentationShortcut"
Name="My Online Documentation"
Target="http://wixtoolset.org/"/>
<RemoveFolder Id="ProgramMenuFolder.My_App_Name" On="uninstall" />
<RegistryKey Root="HKCU" Key="Software\WixSharp\Used">
<RegistryValue Value="0" Type="string" KeyPath="yes" />
</RegistryKey>
</Component>
</Directory>
I don't think it's as difficult or involved as I perhaps made it look. It will just take a little trial and error to get the XPath node locator pointed to the right place to insert your XML. Also, I notice that Wix# XML syntax seems to be a little different (and less complete in this "shortcuts" area) than WiX. (For example, Wix# inserts a element that WiX doesn't, and WiX allows you to specify the start folder and other values for the shortcut more clearly) The example XML I used comes from a Wix# installer I have that adds Start Menu shortcuts. If you want to do a more pure WiX approach for shortcuts, and just inject them all, using this approach, then refer to these WiX links: http://wixtoolset.org/documentation/manual/v3/howtos/files_and_registry/create_start_menu_shortcut.html
http://wixtoolset.org/documentation/manual/v3/howtos/files_and_registry/create_internet_shortcut.html
The pure WiX XML injection approach for shortcuts would have the advantage of allowing you a bit more control over what gets created.
In the Wix# samples, there is a sample in Samples\InjectXML\Setup.cs that also shows this technique.
Have a look at <Wix#>\Samples\Shortcuts in the downloadables.
精彩评论