安装应用程序C#时注册表发生更改

本文关键字:注册表 应用程序 安装 | 更新日期: 2023-09-27 18:00:39

正准备第一次使用visualstudio为应用程序制作安装包。我有一个注册表值,在安装程序时需要更改。每次计算机启动时,我都需要程序启动。

这是一项相当琐碎的任务吗?有人能为我指出一些尝试并实现这一目标的方法吗。

到目前为止,我只有一个带有表单等的应用程序。

安装应用程序C#时注册表发生更改

我也有同样的问题,我正在使用WIX,并遵循这个Stackoverflow问题的答案。我也是WIX的新手,如果你感兴趣的话,这里是我的完整WIX工作脚本(在Windows XP和Windows 8.1上测试):

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="*" 
       Name="FooSetup" Language="1033"
       Version="1.0.1.1" 
       Manufacturer="Foo Enterprise" 
       UpgradeCode="9235c293-2f08-4c2b-b7a5-69d01f5fca32">
    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
    <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
    <MediaTemplate EmbedCab="yes" />

    <!-- Step 1: Define the directory structure -->
    <Directory Id="TARGETDIR" Name="SourceDir">
       <Directory Id="ProgramFilesFolder">
          <Directory Id="INSTALLFOLDER" Name="FooSetup" />
       </Directory>
       <Directory Id="ProgramMenuFolder">
          <Directory Id="ApplicationProgramsFolder" Name="Foo Application"/>
       </Directory>
       <Directory Id="DesktopFolder" Name="Desktop" />
    </Directory>
    <!-- Step 2: Add files to your installer package & add autostart feature -->
    <DirectoryRef Id="INSTALLFOLDER">
       <!-- The main executable file-->
       <Component Id="FooApplication" Guid="3F122E60-3745-4AEB-ADA3-B90DCB87E0BD">
          <File Id="FooMainApp" Source="$(var.Foo.TargetPath)" KeyPath="yes"/>
       </Component>
       <!-- The main lib file-->
       <Component Id="FooLib" Guid="83BEDB02-C9F5-4A06-B3D5-0A4D61D6A99C">
          <File Id="FooFileLib" Source="$(var.Foo.Lib.TargetPath)" KeyPath="yes"/>
       </Component>
       <!-- Register windows autostart registry -->
       <Component Id="RegistryEntries" Guid="45C7AC46-1101-4301-83E1-D24392283A60">
          <RegistryValue Type="string"
                   Name="FooStartup"
                   Value="[#FooMainApp]"
                   Root="HKLM"
                   Key="Software'Microsoft'Windows'CurrentVersion'Run"
                   Action="write"/>
       </Component>
    </DirectoryRef>
    <!-- Step 3: Add the shortcut to your installer package -->
    <!-- Start Menu -->
    <DirectoryRef Id="ApplicationProgramsFolder">
       <Component Id="FooShortcutMenu" Guid="3874D005-4E1C-4C0E-9CEA-8CD8D5442B60">
          <Shortcut Id="FooApplicationStartMenuShortcut"
              Name="Foo Application"
              Description="The Foo is Cool!"
              Target="[#FooMainApp]"
              WorkingDirectory="INSTALLFOLDER"/>
          <RemoveFolder Id="ApplicationProgramsFolder" On="uninstall"/>
          <RegistryValue Root="HKCU" Key="Software'Microsoft'FooApplication" Name="installed" Type="integer" Value="1" KeyPath="yes"/>
       </Component>
    </DirectoryRef>
    <!-- Desktop Menu -->
    <DirectoryRef Id="DesktopFolder">
       <Component Id="FooDesktopShortcutMenu" Guid="D4D0A2ED-C0DB-4524-AC53-D30F904DB846">
          <Shortcut Id="FooApplicationDesktopShortcut"
              Name="Foo Application"
              Description="The Foo is Cool!"
              Target="[#FooMainApp]"
              WorkingDirectory="INSTALLFOLDER"
              Directory="DesktopFolder"/>
          <RemoveFolder Id="DesktopFolder" On="uninstall"/>
          <RegistryValue Root="HKCU" Key="Software'Microsoft'FooApplication" Name="installed" Type="integer" Value="1" KeyPath="yes"/>
       </Component>
    </DirectoryRef>
    <!-- Tell Wix -->
    <Feature Id="ProductFeature" Title="FooSetup" Level="1">
       <ComponentRef Id="FooApplication" />
       <ComponentRef Id="FooLib" />
       <ComponentRef Id="FooShortcutMenu"/>
       <ComponentRef Id="FooDesktopShortcutMenu"/>
       <ComponentRef Id="RegistryEntries" />
    </Feature>
</Product>
</Wix>

这取决于用于创建安装包的工具,通常需要在下添加一个字符串值

HKEY_CURRENT_USER''Software''Microsoft''Windows''CurrentVersion''Run或HKEY_LOCAL_MACHINE''Software''Microsoft''Windows''CurrentVersion''Run

类似于"AppName":"AppPath"您还可以通过在代码中添加regstry来实现这一点。