我该如何使用WiX Burn在Windows 8和Windows Server 2012上安装.NET Framework 3.5?

3

WiX手册包含“如何使用Burn安装.NET Framework”。然而,这些说明似乎不能在Windows 8、Windows Server 2012和后续操作系统上安装.NET Framework 3.5。请参见此StackOverflow问题,尤其是此wix-users邮件列表讨论了解详情。Microsoft .NET Framework 3.5 Service pack 1(完整版)安装程序将无法运行,因为您需要使用部署映像服务和管理器(DISM.exe)或其他技术将所需的框架作为Windows功能启用。建议的命令行如下,假设Windows安装在默认位置:

C:\Windows\system32\dism.exe /online /norestart /enable-feature /featurename:netfx3

有没有一种简单的方法来确保在Windows 8和Windows Server 2012上使用WiX安装程序时安装.NET Framework 3.5?是否有一种好的方式将这一步包含在安装链中?

2
显然,32位的dism.exe在64位Windows上设计为立即失败(而不是像procexp.exe一样将命令转发到64位版本)。因此,要使用dism,您必须在64位Windows上找到64位版本。 %WINDIR%\sysnative\适用于64位Windows上的位置,但不适用于32位Windows。 - Tom Blodget
@TomBlodget,感谢您对在32位系统上使用此方法的警告。我可以确认这是一个问题。 - Dan Jagnow
2个回答

1
这是我能想到的最好方案。我添加了一个片段,可以在Windows 8和Windows Server 2012之前的操作系统上安装.NET Framework 3.5。请注意,这需要引用NetFxExtension来定义NETFRAMEWORK35_SP_LEVEL。
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension" xmlns:netfx="http://schemas.microsoft.com/wix/NetFxExtension">
  <Fragment>
    <util:RegistrySearchRef Id="NETFRAMEWORK35_SP_LEVEL"/>    
    <PackageGroup Id="NetFx35Redist">
      <ExePackage
          SourceFile="{a path on my network}\Microsoft\DotNetFx\3.5\dotnetfx35.exe"
          DisplayName="Microsoft .NET Framework 3.5 Full"
          InstallCondition="VersionNT &lt; v6.1"
          InstallCommand="/q /norestart"
          RepairCommand="/q /norestart /f"
          UninstallCommand="/q /norestart /uninstall"
          PerMachine="yes"
          DetectCondition="NETFRAMEWORK35_SP_LEVEL &gt;= 1"
          Id="dotnetfx35.exe"
          Vital="yes"
          Permanent="yes"
          Protocol="none"
          Compressed="yes"
          Name="redist\dotnetfx35.exe">
        <!-- Exit codes
             0 = Successful installation.
          3010 = Successful installation; however, a system reboot is required.
        -->
        <ExitCode Value="0" Behavior="success" />
        <ExitCode Value="3010" Behavior="forceReboot" />
        <ExitCode Behavior="error"/>
      </ExePackage>
    </PackageGroup>
  </Fragment>
</Wix>

在我的托管引导程序代码中,我正在处理应用阶段开始时的Windows 8 / Windows Server 2012:
model.Bootstrapper.ApplyBegin += this.ApplyBegin;

...

private void ApplyBegin(object sender, ApplyBeginEventArgs e)
{
    this.EnsureNetFramework35();
}

调用dism.exe启用.NET Framework 3.5的方法如下。一些代码引用了像ProgressViewModel这样的类,这些类可能不在每个托管的启动程序实现中,但我希望这为实现自己版本提供了一个有用的起点。
/// <summary>
/// Make sure we have the .NET Framework 3.5 when we're on Windows 8, Windows Server 2012, or later.
/// </summary>
private void EnsureNetFramework35()
{
    // Don't worry if we're on an older OS.  We don't need DISM.exe in that case.
    if (Environment.OSVersion.Version < new Version(6, 1) && this.root.Model.Engine.NumericVariables.Contains("NETFRAMEWORK35_SP_LEVEL"))
    {
        return;
    }

    // Don't worry if .NET Framework 3.5 is already installed.
    if (this.root.Model.Engine.NumericVariables.Contains("NETFRAMEWORK35_SP_LEVEL") &&
        this.root.Model.Engine.NumericVariables["NETFRAMEWORK35_SP_LEVEL"] >= 1)
    {
        return;
    }

    // Enable .NET Framework 3.5.
    this.root.Model.Engine.Log(LogLevel.Standard, "Enabling .NET Framework 3.5.");
    this.root.ProgressViewModel.Message = "Enabling .NET Framework 3.5.";

    // Get the path to DISM.exe.
    string windowsPath = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
    string systemPath = Path.Combine(windowsPath, "System32");
    if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
    {
        // For 32-bit processes on 64-bit systems, %windir%\system32 folder
        // can only be accessed by specifying %windir%\sysnative folder.
        systemPath = Path.Combine(windowsPath, "SysNative");
    }

    string dismPath = Path.Combine(systemPath, @"dism.exe");
    string arguments = "/online /enable-feature:NetFx3 /quiet /norestart";

    if (!File.Exists(dismPath))
    {
        this.root.Model.Engine.Log(LogLevel.Error, "Could not find file: " + dismPath);
        return;
    }

    this.root.Model.Engine.Log(LogLevel.Standard, dismPath + " " + arguments);
    this.root.ProgressViewModel.DetailMessage = dismPath + " " + arguments;

    Process process = new Process();
    process.StartInfo.FileName = dismPath;
    process.StartInfo.Arguments = arguments;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.Start();
    process.WaitForExit();

    // Check to see if we encountered any errors.
    if (process.ExitCode == 0)
    {
        this.root.Model.Engine.Log(LogLevel.Standard, ".NET Framework 3.5 enabled.");
        this.root.ProgressViewModel.Message = ".NET Framework 3.5 enabled.";
        this.root.ProgressViewModel.DetailMessage = string.Empty;
    }
    else
    {
        this.root.Model.Engine.Log(LogLevel.Error, ".NET Framework 3.5 could not be enabled.  Exit code: " + process.ExitCode);
        this.root.ProgressViewModel.Message = ".NET Framework 3.5 could not be enabled.";
        this.root.ProgressViewModel.DetailMessage = string.Empty;
    }
}

0

谢谢,@LeoN,但正如我在问题中提到的那样,这些说明似乎不适用于Windows 8和Windows Server 2012。 - Dan Jagnow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接