Writing files to external flash drive in Windows Universal

风流意气都作罢 提交于 2019-11-30 16:20:17

问题


I'm writing an application using Windows IoT on a Raspberry PI. I would like to write data to an external flash drive connected to one of the USB ports. I've found examples on how to write to the SD card in the PI, but the SD card won't be accessible in the final product.

I can get the root folder name of the flash drive, but when I try to write a file to it, I get an access denied message. If I switch to the SD card everything works fine.

Can anyone point me to an example that allows access to an external flash drive?


回答1:


For security reasons, Universal Windows Applications can only have access to certain types of files on external drives,

  • Music
  • Picture
  • Video

And you have to explicitly declare it in the Package.appxmanifest file.

  • Music Library
  • Picture Library
  • Video Library

You might also want to check the Removable Storage capability as well.

I don't think you have access to a general file format except the above three types, otherwise you'll get an "Access is denied" exception.

Find more details in here.

Once you have your capabilities declared, you can get the root folder for your external storage device with the following code,

var removableDevices = KnownFolders.RemovableDevices;
var externalDrives = await removableDevices.GetFoldersAsync();
var drive0 = externalDrives[0];

Then you can use the Stream methods to write to a file, following the code samples in here.

If you want to write data to an generic file format, an workaround is to use an accessible file format(like jpg), and write your raw data to it. Below is some code sample that is verified on Raspberry Pi 2 Model B, with Windows IoT 14393, with an external USB drive connected to the USB port.

    private async void WriteData()
    {
        var removableDevices = KnownFolders.RemovableDevices;
        var externalDrives = await removableDevices.GetFoldersAsync();
        var drive0 = externalDrives[0];

        var testFolder = await drive0.CreateFolderAsync("Test");
        var testFile = await testFolder.CreateFileAsync("Test.jpg");

        var byteArray = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
        using (var sourceStream = new MemoryStream(byteArray).AsRandomAccessStream())
        {
            using (var destinationStream = (await testFile.OpenAsync(FileAccessMode.ReadWrite)).GetOutputStreamAt(0))
            {
                await RandomAccessStream.CopyAndCloseAsync(sourceStream, destinationStream);
            }
        }
    }



回答2:


Set Capability in Package.appxmanifest file

 <Capabilities>
    <Capability Name="internetClient" />
    <uap:Capability Name="removableStorage" />
    <!--When the device's classId is FF * *, there is a predefined name for the class. 
          You can use the name instead of the class id. 
          There are also other predefined names that correspond to a classId.-->
    <DeviceCapability Name="usb">
      <!--SuperMutt Device-->
      <Device Id="vidpid:045E 0611">
        <!--<wb:Function Type="classId:ff * *"/>-->
        <Function Type="name:vendorSpecific" />
      </Device>
    </DeviceCapability>
  </Capabilities>

private async void btnCopyImages_Click(object sender, RoutedEventArgs e)
        {

            // Get the logical root folder for all external storage devices.
            StorageFolder externalDevices = Windows.Storage.KnownFolders.RemovableDevices;
            // Get the first child folder, which represents the SD card.
            StorageFolder sdCard = (await externalDevices.GetFoldersAsync()).FirstOrDefault();
            // An SD card is present and the sdCard variable now contains a to reference it.
            if (sdCard != null)
            {
                StorageFile resultfile = await sdCard.CreateFileAsync("foo.png", CreationCollisionOption.GenerateUniqueName);
                 string base64 = "/9j/4AAQSkZJRgABAQEAYABgAAD/4RjqR.....;
                 var bytes = Convert.FromBase64String(base64);
                await FileIO.WriteBytesAsync(resultfile, bytes);
         }
        // No SD card is present.
          else
             {
             }
}


来源:https://stackoverflow.com/questions/38859872/writing-files-to-external-flash-drive-in-windows-universal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!