构建MSI以安装DLL并使其可以从VS中的“添加引用”访问

时间:2010-03-26 21:32:14

标签: .net dll visual-studio-2010 assemblies managed

我想构建一个安装托管DLL的MSI安装程序,并使用户可以从解决方案资源管理器中的Visual Studio的“添加引用”菜单访问它。我相信我应该在Windows注册表中添加一个密钥,但我无法看到如何告诉VS项目MSI在安装时执行此操作。

这是怎么做的,我应该在哪里寻找关于这类事情的教程?

1 个答案:

答案 0 :(得分:1)

转到以下注册表项:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders

添加一个子项(其名称无关紧要,但也可能是描述性的),其默认值为包含程序集的文件夹,您应该好好去。

如何使用WiX添加所需的密钥

这是WiX代码的精简片段,用于创建所需的密钥并将其默认值设置为安装程序集的文件夹。 这不是一个完整的WiX安装程序,无法直接粘贴 - 我删除了Guid属性,只显示了目录,组件和功能声明。一旦你看到一些完整的WiX代码,它应该是合理的明确如何合并这些位。

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <!-- Path from WiX file to the DLL(s) you're installing -->
  <?define BuildPath="..\Bin\Release" ?>
  <!-- The base key path - note no HKLM -->
  <?define AssemblyFolders = "Software\Microsoft\.NETFramework\AssemblyFolders" ?>

  <Product>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="CompanyFolder" Name="YourCompany">
          <Directory Id="INSTALLLOCATION" Name="Your Product">
            <!-- Id="AssemblyFolder" captures the location where the DLL gets installed -->
            <Directory Id="AssemblyFolder" Name="Bin">
              <Component Id="AssemblyComponent">
                <!-- This is the bit that copies the assembly file -->
                <File Id="YourDll.dll" Checksum="yes" Source="$(var.BuildPath)YourDll.dll" />
              </Component>
              <Component Id="ControlRegistrationComponent">
                <!-- This is the bit that creates the registry key -->
                <RegistryKey Action="createAndRemoveOnUninstall" Root="HKLM" Key="$(var.AssemblyFolders)\Your Product Name">
                  <!-- Square brackets result in a reference -- so the install folder gets picked up -->
                  <RegistryValue Action="write" Value="[AssemblyFolder]" Type="string" />
                </RegistryKey>
              </Component>
            </Directory>
          </Directory>
        </Directory>
      </Directory>
    </Directory>

    <Feature Id="PRODUCTROOTFEATURE">
      <!-- This causes the assembly to be installed -->
      <ComponentRef Id="AssemblyComponent" />
      <!-- This causes the registry key to be created -->
      <ComponentRef Id="ControlRegistrationComponent" />
    </Feature>

  </Product>
</Wix>

要在获得完整的WiX脚本后构建MSI,您可以使用批处理文件,其核心如下所示:

candle.exe "%WXS_NAME%.wxs" -out "%WXS_NAME%.wixobj"
light.exe "%WXS_NAME%.wixobj" -out "%WXS_NAME%.msi"

其中%WXS_NAME%是WiX .wxs源文件的名称。

(我意识到这可能看起来有点神秘但是(a)有很多WiX示例,教程和参考资料可以帮助您入门;(b)Visual Studio的Votive插件可以保护您免受一些胆量无论如何。)

要自动构建MSI,请使用Votive加载项(WiX的一部分),或构建后步骤或MSBuild构建脚本。我们使用构建脚本,因为我们通常只想将MSI构建为自动构建的一部分 - 在构建后的步骤中执行它会减慢开发构建速度 - 但您的里程可能会有所不同。