You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用AddPackageByAppInstallerFileAsync遇SEHException(错误码-2147467259)求助

问题描述

我正尝试按照微软文档通过代码更新我的MSIX打包WinForms应用。我的包与.appinstaller文件一同发布在网络共享路径下,手动从.appinstaller安装/更新以及Windows启动时自动更新均正常,但通过代码发起更新时,出现了SEHException异常"外部组件已抛出异常",错误码为-2147467259,我认为这是某种"文件未找到"错误。我的代码如下:

var pm = new PackageManager();
await pm.AddPackageByAppInstallerFileAsync(Package.Current.GetAppInstallerInfo().Uri,
          AddPackageByAppInstallerOptions.ForceTargetAppShutdown,             
          pm.GetDefaultPackageVolume());

我已确认Package.Current.GetAppInstallerInfo().Uri的值正确——将该值粘贴到“运行”对话框可启动安装程序。是包托管在共享驱动器存在问题,还是我操作有误?

补充:我也尝试发布到网站,仍出现相同错误。

解决方案

1. 补充清单权限声明

MSIX打包的桌面应用调用PackageManager类的更新API,必须在Package.appxmanifest中添加受限权限声明,否则会因权限不足触发E_FAIL错误(对应你看到的-2147467259错误码)。请确保清单包含以下内容:

<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
         xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities">
  <!-- 其他原有清单内容 -->
  <Capabilities>
    <rescap:Capability Name="packageManagement" />
  </Capabilities>
</Package>

2. 替换为更新专用API

AddPackageByAppInstallerFileAsync更偏向于初始安装场景,触发更新推荐使用AppInstallerManager的专用方法(需先安装Microsoft.Windows.AppLifecycle NuGet包),代码示例:

using Microsoft.Windows.AppInstallerManager;

var appInstallerUri = Package.Current.GetAppInstallerInfo().Uri;
var installerManager = AppInstallerManager.GetDefault();

// 检查并安装更新
var updateResult = await installerManager.CheckForUpdatesAndInstallAsync(
    appInstallerUri,
    AppInstallerUpdateOptions.ForceAppShutdown);

// 根据结果处理后续逻辑
if (updateResult.Status == AppInstallerUpdateStatus.CompletedSuccessfully)
{
    // 更新完成,可提示用户重启应用
}

这个API专门针对.appinstaller的更新流程,适配性更强。

3. 排查潜在配置问题

  • 核对.appinstaller文件内的MainPackage和UpdateLocation节点,确保包路径配置与实际发布路径一致;
  • 检查系统是否安装了最新版的App Installer组件(可通过微软商店更新),旧版本可能存在API兼容性问题;
  • 避免在应用启动初期立即调用更新API,确保应用完全初始化后再执行更新操作。

内容的提问来源于stack exchange,提问作者BenV

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 20:25:34