首页 > 解决方案 > 如何配置 WIX 安装程序以使 exe 在 Windows 启动时自动启动

问题描述

我最近制作了 C# 项目并尝试在 Windows 启动时运行它。我配置了 WXS 文件以编写注册表运行键 (HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run) 下面是我的 WXS 文件。

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product Id="*" Name="NACViewAgentSetupProject" Language="1033" Version="1.0.0.0" Manufacturer="JSGURU" UpgradeCode="MYGUID">
        <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

        <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
        <MediaTemplate />

        <Feature Id="ProductFeature" Title="NACViewAgentSetupProject" Level="1">
            <ComponentGroupRef Id="ProductComponents" />
        </Feature>
    </Product>

    <Fragment>
        <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="ProgramFilesFolder">
                <Directory Id="INSTALLFOLDER" Name="NACViewAgentSetupProject" />
            </Directory>
      <Directory Id="ProgramMenuFolder"></Directory>
        </Directory>
    </Fragment>

  <Fragment>
    <DirectoryRef Id="ProgramMenuFolder">
      <Component Id="ApplicationShortcut" Guid="MYGUID">
        <Shortcut Id="ApplicationStartMenuShortcut" Name="NACViewAgent" Description="NACVIEW Agent" Target="[INSTALLFOLDER]NACViewAgent.exe" WorkingDirectory="INSTALLFOLDER">
          <!--AUMID-->
          <ShortcutProperty Key="System.AppUserModel.ID" Value="NACVIEW.NACViewAgent"/>

          <!--COM CLSID, specifying which CLSID to activate when toast clicked-->
          <ShortcutProperty Key="System.AppUserModel.ToastActivatorCLSID" Value="{MYGUID}"/>
        </Shortcut>

        <RemoveFile Id="RemoveApplicationShortcut" Directory="ProgramMenuFolder" Name="NACViewAgent" On="uninstall"/>
        <RegistryValue Root="HKCU" Key="Software\NACVIEW" Name="installed" Type="integer" Value="1" KeyPath="yes" />

      </Component>
      <Component Id="ApplicationAutostart" Guid="MYGUID">
        <RegistryValue Root="HKCU" Action="write" Key="Software\Microsoft\Windows\CurrentVersion\Run" Name="NACViewAgent" Value="[INSTALLFOLDER]NACViewAgent.exe" Type="string"/>
      </Component>
    </DirectoryRef>
  </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> -->

      <Component Id="NACViewAgent.exe" Guid="MYGUID">
        <File Id="NACViewAgent.exe" Name="NACViewAgent.exe" Source="$(var.NACViewAgent.TargetDir)NACViewAgent.exe"/>
      </Component>
      <Component Id="GalaSoft.MvvmLight.dll" Guid="MYGUID">
        <File Id="GalaSoft.MvvmLight.dll" Name="GalaSoft.MvvmLight.dll" Source="$(var.NACViewAgent.TargetDir)GalaSoft.MvvmLight.dll"/>
      </Component>
      <Component Id="Newtonsoft.Json.dll" Guid="MYGUID">
        <File Id="Newtonsoft.Json.dll" Name="Newtonsoft.Json.dll" Source="$(var.NACViewAgent.TargetDir)Newtonsoft.Json.dll"/>
      </Component>
      <Component Id="Microsoft.WindowsAPICodePack.dll" Guid="MYGUID">
        <File Id="Microsoft.WindowsAPICodePack.dll" Name="Microsoft.WindowsAPICodePack.dll" Source="$(var.NACViewAgent.TargetDir)Microsoft.WindowsAPICodePack.dll"/>
      </Component>
      <Component Id="CommonServiceLocator.dll" Guid="MYGUID">
        <File Id="CommonServiceLocator.dll" Name="CommonServiceLocator.dll" Source="$(var.NACViewAgent.TargetDir)CommonServiceLocator.dll"/>
      </Component>
      <Component Id="System.Windows.Interactivity.dll" Guid="MYGUID">
        <File Id="System.Windows.Interactivity.dll" Name="System.Windows.Interactivity.dll" Source="$(var.NACViewAgent.TargetDir)System.Windows.Interactivity.dll"/>
      </Component>
      <Component Id="GalaSoft.MvvmLight.Extras.dll" Guid="MYGUID">
        <File Id="GalaSoft.MvvmLight.Extras.dll" Name="GalaSoft.MvvmLight.Extras.dll" Source="$(var.NACViewAgent.TargetDir)GalaSoft.MvvmLight.Extras.dll"/>
      </Component>
      <Component Id="GalaSoft.MvvmLight.Platform.dll" Guid="MYGUID">
        <File Id="GalaSoft.MvvmLight.Platform.dll" Name="GalaSoft.MvvmLight.Platform.dll" Source="$(var.NACViewAgent.TargetDir)GalaSoft.MvvmLight.Platform.dll"/>
      </Component>
      <Component Id="Microsoft.WindowsAPICodePack.ExtendedLinguisticServices.dll" Guid="MYGUID">
        <File Id="Microsoft.WindowsAPICodePack.ExtendedLinguisticServices.dll" Name="Microsoft.WindowsAPICodePack.ExtendedLinguisticServices.dll" Source="$(var.NACViewAgent.TargetDir)Microsoft.WindowsAPICodePack.ExtendedLinguisticServices.dll"/>
      </Component>
      <Component Id="Microsoft.WindowsAPICodePack.Sensors.dll" Guid="MYGUID">
        <File Id="Microsoft.WindowsAPICodePack.Sensors.dll" Name="Microsoft.WindowsAPICodePack.Sensors.dll" Source="$(var.NACViewAgent.TargetDir)Microsoft.WindowsAPICodePack.Sensors.dll"/>
      </Component>
      <Component Id="Microsoft.WindowsAPICodePack.Shell.dll" Guid="MYGUID">
        <File Id="Microsoft.WindowsAPICodePack.Shell.dll" Name="Microsoft.WindowsAPICodePack.Shell.dll" Source="$(var.NACViewAgent.TargetDir)Microsoft.WindowsAPICodePack.Shell.dll"/>
      </Component>
      <Component Id="Microsoft.WindowsAPICodePack.ShellExtensions.dll" Guid="MYGUID">
        <File Id="Microsoft.WindowsAPICodePack.ShellExtensions.dll" Name="Microsoft.WindowsAPICodePack.ShellExtensions.dll" Source="$(var.NACViewAgent.TargetDir)Microsoft.WindowsAPICodePack.ShellExtensions.dll"/>
      </Component>

      <!--Tell WiX to install the shortcut-->
      <ComponentRef Id="ApplicationShortcut"/>
      <ComponentRef Id="ApplicationAutostart"/>
        </ComponentGroup>
    </Fragment>
</Wix>

构建 wix 项目并安装它,然后我可以在 Run 键中看到 NACViewAgent 值,如下所示。 在此处输入图像描述

我重新启动了我的机器。但不幸的是,当 Windows 启动时,我的 exe 不会自动启动。

提前致谢!

标签: c#.netwixregistry

解决方案


推荐阅读