【问题标题】:Writing files to external flash drive in Windows Universal在 Windows Universal 中将文件写入外部闪存驱动器
【发布时间】:2016-08-09 20:30:21
【问题描述】:

我正在 Raspberry PI 上使用 Windows IoT 编写应用程序。我想将数据写入连接到 USB 端口之一的外部闪存驱动器。我在 PI 中找到了有关如何写入 SD 卡的示例,但最终产品中无法访问 SD 卡。

我可以获得闪存驱动器的根文件夹名称,但是当我尝试向其中写入文件时,我收到拒绝访问消息。如果我切换到 SD 卡,一切正常。

谁能指出一个允许访问外部闪存驱动器的示例?

【问题讨论】:

  • 一些示例代码将有助于解决问题。

标签: c# windowsiot


【解决方案1】:

出于安全原因,通用 Windows 应用程序只能访问外部驱动器上某些类型的文件,

  • 音乐
  • 图片
  • 视频

而且你必须在 Package.appxmanifest 文件中明确声明它。

  • 音乐库
  • 图片库
  • 视频库

您可能还想检查可移动存储功能。

我认为您无法访问除上述三种类型之外的通用文件格式,否则您将收到“访问被拒绝”异常。

here 中查找更多详细信息。

一旦你声明了你的能力,你可以使用以下代码获取外部存储设备的根文件夹,

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

然后您可以按照here 中的代码示例,使用 Stream 方法写入文件。

如果您想将数据写入通用文件格式,一种解决方法是使用可访问的文件格式(如 jpg),并将原始数据写入其中。下面是一些在 Raspberry Pi 2 Model B 上验证的代码示例,使用 Windows IoT 14393,外部 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);
            }
        }
    }

【讨论】:

  • 非常感谢。通过在应用程序清单文件中添加文件关联,我也能够创建一个 txt 文件。但是它不允许 csv 文件。
  • 我收回 csv 文件的评论。它也适用于该文件类型。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2018-08-24
    • 1970-01-01
    • 2014-06-28
    • 2017-04-08
    • 1970-01-01
    • 2019-08-03
    • 2020-12-10
    • 2022-07-01
    • 1970-01-01
    相关资源
    最近更新 更多