简体   繁体   English

在Windows Universal中将文件写入外部闪存驱动器

[英]Writing files to external flash drive in Windows Universal

I'm writing an application using Windows IoT on a Raspberry PI. 我正在Raspberry PI上使用Windows IoT编写应用程序。 I would like to write data to an external flash drive connected to one of the USB ports. 我想将数据写入连接到USB端口之一的外部闪存驱动器。 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. 我已经找到了有关如何在PI中写入SD卡的示例,但是在最终产品中将无法访问SD卡。

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. 如果我切换到SD卡,则一切正常。

Can anyone point me to an example that allows access to an external flash drive? 谁能指出一个允许访问外部闪存驱动器的示例?

Set Capability in Package.appxmanifest file 在Package.appxmanifest文件中设置功能

 <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
             {
             }
}

For security reasons, Universal Windows Applications can only have access to certain types of files on external drives, 出于安全原因,通用Windows应用程序只能访问外部驱动器上的某些类型的文件,

  • Music 音乐
  • Picture 图片
  • Video 视频

And you have to explicitly declare it in the Package.appxmanifest file. 并且您必须在Package.appxmanifest文件中明确声明它。

  • 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 . 然后,您可以按照此处的代码示例,使用Stream方法写入文件。

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. 如果要将数据写入通用文件格式,一种解决方法是使用可访问的文件格式(如jpg),然后将原始数据写入其中。 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. 以下是一些代码示例,已在具有Windows IoT 14393的Raspberry Pi 2 Model B上进行了验证,并且外部USB驱动器已连接到USB端口。

    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);
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM