title: UWP application unable to access USB drive even though permissions are set
body: <p>I am working on an app that is responsible for formatting a USB drive and prepare it for further use on an embedded system.</p>
  <p>I am formatting the drive using the following method that I found on stack overflow (unfortunately I did not save the link. I'll post it there if I find it again)</p>
  <pre class="lang-cs prettyprint-override"><code>public static bool FormatUSB(string driveLetter, string fileSystem = &quot;FAT32&quot;, bool quickFormat = true,
                                     int clusterSize = 4096, string label = &quot;USB_0000&quot;, bool enableCompression = false)
          {
              //add logic to format Usb drive
              //verify conditions for the letter format: driveLetter[0] must be letter. driveLetter[1] must be &quot;:&quot; 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(@&quot;select * from Win32_Volume WHERE DriveLetter = '&quot; + driveLetter + &quot;'&quot;);
              foreach (ManagementObject vi in searcher.Get())
              {
                  try
                  {
                      var completed = false;
                      var watcher = new ManagementOperationObserver();
  
                      watcher.Completed += (sender, args) =&gt;
                      {
                          Console.WriteLine(&quot;USB format completed &quot; + args.Status);
                          completed = true;
                      };
                      watcher.Progress += (sender, args) =&gt;
                      {
                          Console.WriteLine(&quot;USB format in progress &quot; + args.Current);
                      };
  
                      vi.InvokeMethod(watcher, &quot;Format&quot;, new object[] { fileSystem, quickFormat, clusterSize, label, enableCompression });
  
                      while (!completed) { System.Threading.Thread.Sleep(1000); }
  
  
                  }
                  catch
                  {
  
                  }
              }
              return true;
          }
  </code></pre>
  <p>I also added all the capabilities that should be required (I think) in order to access a removable drive in my manifest:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  
  &lt;Package
    xmlns=&quot;http://schemas.microsoft.com/appx/manifest/foundation/windows10&quot;
    xmlns:mp=&quot;http://schemas.microsoft.com/appx/2014/phone/manifest&quot;
    xmlns:uap=&quot;http://schemas.microsoft.com/appx/manifest/uap/windows10&quot;
    xmlns:rescap=&quot;http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities&quot;
    xmlns:iot=&quot;http://schemas.microsoft.com/appx/manifest/iot/windows10&quot;
    IgnorableNamespaces=&quot;uap mp rescap iot&quot;&gt;
  
    &lt;Identity
      Name=&quot;7b9becad-6afd-4872-bcb7-7f414c098edf&quot;
      Publisher=&quot;CN=vitto&quot;
      Version=&quot;1.0.0.0&quot; /&gt;
  
    &lt;mp:PhoneIdentity PhoneProductId=&quot;7b9becad-6afd-4872-bcb7-7f414c098edf&quot; PhonePublisherId=&quot;00000000-0000-0000-0000-000000000000&quot;/&gt;
  
    &lt;Properties&gt;
      &lt;DisplayName&gt;DiskMakerApp&lt;/DisplayName&gt;
      &lt;PublisherDisplayName&gt;vitto&lt;/PublisherDisplayName&gt;
      &lt;Logo&gt;Assets\StoreLogo.png&lt;/Logo&gt;
    &lt;/Properties&gt;
  
    &lt;Dependencies&gt;
      &lt;TargetDeviceFamily Name=&quot;Windows.Universal&quot; MinVersion=&quot;10.0.0.0&quot; MaxVersionTested=&quot;10.0.0.0&quot; /&gt;
    &lt;/Dependencies&gt;
  
    &lt;Resources&gt;
      &lt;Resource Language=&quot;x-generate&quot;/&gt;
    &lt;/Resources&gt;
  
    &lt;Applications&gt;
      &lt;Application Id=&quot;App&quot;
        Executable=&quot;$targetnametoken$.exe&quot;
        EntryPoint=&quot;DiskMakerApp.App&quot;&gt;
        &lt;uap:VisualElements
          DisplayName=&quot;DiskMakerApp&quot;
          Square150x150Logo=&quot;Assets\Square150x150Logo.png&quot;
          Square44x44Logo=&quot;Assets\Square44x44Logo.png&quot;
          Description=&quot;DiskMakerApp&quot;
          BackgroundColor=&quot;transparent&quot;&gt;
          &lt;uap:DefaultTile Wide310x150Logo=&quot;Assets\Wide310x150Logo.png&quot;/&gt;
          &lt;uap:SplashScreen Image=&quot;Assets\SplashScreen.png&quot; /&gt;
        &lt;/uap:VisualElements&gt;
      &lt;/Application&gt;
    &lt;/Applications&gt;
  
    &lt;Capabilities&gt;
        &lt;rescap:Capability Name=&quot;broadFileSystemAccess&quot; /&gt;
        &lt;rescap:Capability Name=&quot;appCaptureSettings&quot; /&gt;
        &lt;Capability Name=&quot;internetClient&quot; /&gt;
      &lt;uap:Capability Name=&quot;removableStorage&quot; /&gt;
        &lt;iot:Capability Name=&quot;systemManagement&quot;/&gt;
        &lt;DeviceCapability Name=&quot;usb&quot;/&gt;
    &lt;/Capabilities&gt;
  &lt;/Package&gt;
  </code></pre>
  <p>And also allowed access to the file system in Window's settings page:
  <a href="https://i.stack.imgur.com/ohKjb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ohKjb.png" alt="enter image description here" /></a></p>
  <p>But I am still getting:
  <a href="https://i.stack.imgur.com/ZkS6c.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZkS6c.png" alt="enter image description here" /></a></p>
  <p>I am wondering if I am missing anything. Is there a way I could run the app as an administrator^ Would that solve the issue? (In any case, only admins would be able to run that app in a real life scenario)</p>