即使设置了权限,UWP 应用程序也无法访问 USB 驱动器

UWP application unable to access USB drive even though permissions are set

我正在开发一个应用程序,负责格式化 USB 驱动器并准备在嵌入式系统上进一步使用。

我正在使用在堆栈溢出时发现的以下方法格式化驱动器(不幸的是我没有保存 link。如果我再次找到它,我会 post 它)

public static bool FormatUSB(string driveLetter, string fileSystem = "FAT32", bool quickFormat = true,
                                   int clusterSize = 4096, string label = "USB_0000", bool enableCompression = false)
        {
            //add logic to format Usb drive
            //verify conditions for the letter format: driveLetter[0] must be letter. driveLetter[1] must be ":" and all the characters mustn't be more than 2
            if (driveLetter.Length != 2 || driveLetter[1] != ':' || !char.IsLetter(driveLetter[0]))
                return false;

            //query and format given drive 
            //best option is to use ManagementObjectSearcher

            var files = Directory.GetFiles(driveLetter);
            var directories = Directory.GetDirectories(driveLetter);

            foreach (var item in files)
            {
                try
                {
                    File.Delete(item);
                }
                catch (UnauthorizedAccessException) { }
                catch (IOException) { }
            }

            foreach (var item in directories)
            {
                try
                {
                    Directory.Delete(item);
                }
                catch (UnauthorizedAccessException) { }
                catch (IOException) { }
            }

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"select * from Win32_Volume WHERE DriveLetter = '" + driveLetter + "'");
            foreach (ManagementObject vi in searcher.Get())
            {
                try
                {
                    var completed = false;
                    var watcher = new ManagementOperationObserver();

                    watcher.Completed += (sender, args) =>
                    {
                        Console.WriteLine("USB format completed " + args.Status);
                        completed = true;
                    };
                    watcher.Progress += (sender, args) =>
                    {
                        Console.WriteLine("USB format in progress " + args.Current);
                    };

                    vi.InvokeMethod(watcher, "Format", new object[] { fileSystem, quickFormat, clusterSize, label, enableCompression });

                    while (!completed) { System.Threading.Thread.Sleep(1000); }


                }
                catch
                {

                }
            }
            return true;
        }

我还在清单中添加了(我认为)访问可移动驱动器所需的所有功能:

<?xml version="1.0" encoding="utf-8"?>

<Package
  xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
  xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
  xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10"
  IgnorableNamespaces="uap mp rescap iot">

  <Identity
    Name="7b9becad-6afd-4872-bcb7-7f414c098edf"
    Publisher="CN=vitto"
    Version="1.0.0.0" />

  <mp:PhoneIdentity PhoneProductId="7b9becad-6afd-4872-bcb7-7f414c098edf" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

  <Properties>
    <DisplayName>DiskMakerApp</DisplayName>
    <PublisherDisplayName>vitto</PublisherDisplayName>
    <Logo>Assets\StoreLogo.png</Logo>
  </Properties>

  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
  </Dependencies>

  <Resources>
    <Resource Language="x-generate"/>
  </Resources>

  <Applications>
    <Application Id="App"
      Executable="$targetnametoken$.exe"
      EntryPoint="DiskMakerApp.App">
      <uap:VisualElements
        DisplayName="DiskMakerApp"
        Square150x150Logo="Assets\Square150x150Logo.png"
        Square44x44Logo="Assets\Square44x44Logo.png"
        Description="DiskMakerApp"
        BackgroundColor="transparent">
        <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png"/>
        <uap:SplashScreen Image="Assets\SplashScreen.png" />
      </uap:VisualElements>
    </Application>
  </Applications>

  <Capabilities>
      <rescap:Capability Name="broadFileSystemAccess" />
      <rescap:Capability Name="appCaptureSettings" />
      <Capability Name="internetClient" />
    <uap:Capability Name="removableStorage" />
      <iot:Capability Name="systemManagement"/>
      <DeviceCapability Name="usb"/>
  </Capabilities>
</Package>

并且还允许访问 Window 的设置页面中的文件系统:

但我仍然得到:

我想知道我是否遗漏了什么。有什么方法可以让我以管理员身份 运行 应用程序^ 这能解决问题吗? (无论如何,只有管理员才能 运行 在现实生活中使用该应用程序)

UWP application unable to access USB drive even though permissions are set

Directory.GetFiles can't not use to access file with path in UWP platform. And you can only use Windows Storage API to access file with path (enable broadFileSystemAccess ), by the way, System.Management Namespace 不适用于 UWP 平台,如果您想在 UWP 应用程序中格式化 USB 设备,请使用桌面扩展来处理。有关更多信息,请参阅 stefan 的博客 UWP with Desktop Extension