如何使用 WixSharp 创建互联网快捷方式

发布于 2024-10-21 19:45:08 字数 87 浏览 9 评论 0原文

我正在使用 WixSharp 来组合安装程序。我希望在 Program Files\ 菜单中有一个快捷方式来打开网页。我可以用 WixSharp 做到这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盗琴音 2024-10-28 19:45:08

在 WixSharp 中,您可以通过 InternetShortcut 类。

下面是我正在开发的一个应用程序的示例,该应用程序通过 InternetShortcut 类添加指向包含图标的网站的链接,并将该链接放置在桌面和开始菜单上。

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;

另请参阅:

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 = 
quot;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 = 
quot;Admin Page",
            Target = "http://localhost:4444",
            Type = InternetShortcut.ShortcutType.link,
            AttributesDefinition = @"IconFile=[INSTALLDIR]\icon.ico;IconIndex=0"
        })
    ); 

project.InstallScope = InstallScope.perMachine;

See also:

黑色毁心梦 2024-10-28 19:45:08

使用 Wix# XML 注入功能将 Internet 快捷方式的 WiX 代码放入您的构建中。使用此 WiX 语法示例作为 Internet 快捷方式:

<util:InternetShortcut Id="OnlineDocumentationShortcut"
                Name="My Online Documentation"
                       Target="http://wixtoolset.org/"/>

在 Wix# 安装程序代码中,首先在主代码中,您将向“WixSourceGenerate”事件添加一个处理程序,该事件在创建 .wxs 文件之后但之前触发已编译。该代码将如下所示:

    // 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);

然后在您的委托方法中,您将拥有如下所示的代码:

/// <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/")));
}

您将需要查看生成的 .wxs 文件,该文件将与生成的 MSI 文件位于同一文件夹中,然后图找出 XPath 是什么,以便“document.Root.Select()”到达要添加插入的 WiX XML 的节点。在我的 wxs 文件中,“开始”菜单快捷方式位于 XML 的一部分,如下所示:

<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>    

因此,要在其中添加 Internet 快捷方式,您会希望生成的 XML 看起来像这样:

<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>

我认为这并不困难或也许正如我所表现的那样,参与其中。只需进行一些尝试和错误即可将 XPath 节点定位器指向插入 XML 的正确位置。另外,我注意到 Wix# XML 语法似乎与 WiX 略有不同(并且在这个“快捷方式”区域不太完整)。 (例如,Wix# 插入了 WiX 没有的元素,并且 WiX 允许您更清楚地指定快捷方式的开始文件夹和其他值)我使用的示例 XML 来自我拥有的添加“开始”菜单的 Wix# 安装程序快捷方式。如果您想对快捷方式采用更纯粹的 WiX 方法,并使用此方法将它们全部注入,请参考以下 WiX 链接:
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

用于快捷方式的纯 WiX XML 注入方法的优点是允许您对创建的内容有更多的控制。

在 Wix# 示例中,Samples\InjectXML\Setup.cs 中有一个示例也展示了此技术。

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.

翻了热茶 2024-10-28 19:45:08

查看可下载内容中的\Samples\Shortcuts。

Have a look at <Wix#>\Samples\Shortcuts in the downloadables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文