首页 > 解决方案 > 包管理器 > 系统找不到指定的文件。(来自 HRESULT 的异常:0x80070002)

问题描述

为我的用户获取软件包时,我在某些 InstalledLocation 中收到“系统找不到指定的文件”。有谁知道为什么会这样?这个应用程序坏了吗?我需要获取 InstalledLocation,无论如何我可以在没有 try catch 块的情况下执行此操作吗?提前致谢

using System;
using System.IO;
using System.Security.Principal;
using Windows.Management.Deployment;
namespace ConsoleApp19
{
    class Program
    {
        static void Main(string[] args)
        {
            NTAccount acc = new NTAccount(WindowsIdentity.GetCurrent().Name);
            SecurityIdentifier sid = (SecurityIdentifier)acc.Translate(typeof(SecurityIdentifier));
            var packages = new PackageManager().FindPackagesForUser(sid.ToString());
            foreach (var package in packages)
            {
                Console.WriteLine(package.Id.FamilyName);
                try
                {
                    Console.WriteLine(package.InstalledLocation.Path);
                }
                catch (FileNotFoundException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            Console.ReadKey();
        }
    }
}

https://docs.microsoft.com/en-us/uwp/api/windows.management.deployment.packagemanager.findpackagesforuser

标签: c#.netwindowsuwp

解决方案


某些以开发模式安装的应用程序在访问 installedLocation 时会抛出错误。为了避免这种情况,只需检查他们是否在开发模式下进食

if (!package.IsDevelopmentMode)
{
}

推荐阅读