我在Raspberry PI上使用Windows IoT编写应用程序。 我想将数据写入连接到其中一个USB端口的外部闪存驱动器。 我已经找到了如何在PI中写入SD卡的示例,但最终产品中无法访问SD卡。
我可以获取闪存驱动器的根文件夹名称,但是当我尝试向其写入文件时,我收到了拒绝访问的消息。 如果我切换到SD卡一切正常。
有人能指出一个允许访问外部闪存驱动器的示例吗?
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?
最满意答案
出于安全原因,Universal Windows Applications只能访问外部驱动器上的某些类型的文件,
音乐 图片 视频您必须在Package.appxmanifest文件中明确声明它。
曲库 图片库 视频库您可能还想检查可移动存储功能。
我不认为您可以访问除上述三种类型之外的常规文件格式,否则您将获得“访问被拒绝”异常。
在此处查找更多详细信息。
声明了您的功能后,您可以使用以下代码获取外部存储设备的根文件夹,
var removableDevices = KnownFolders.RemovableDevices; var externalDrives = await removableDevices.GetFoldersAsync(); var drive0 = externalDrives[0];然后,您可以使用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); } } }For security reasons, Universal Windows Applications can only have access to certain types of files on external drives,
Music Picture VideoAnd you have to explicitly declare it in the Package.appxmanifest file.
Music Library Picture Library Video LibraryYou 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); } } }更多推荐
发布评论