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

C# 在Windows Universal中将文件写入外部闪存驱动器,c#,windowsiot,C#,Windowsiot,我正在用树莓圆周率写一个应用程序。我想将数据写入连接到其中一个USB端口的外部闪存驱动器。我已经找到了如何在PI中写入SD卡的示例,但是在最终产品中无法访问SD卡 我可以获取闪存驱动器的根文件夹名称,但当我尝试向其写入文件时,会收到一条拒绝访问的消息。如果我切换到SD卡,一切都正常 有人能给我举一个允许访问外部闪存驱动器的例子吗?出于安全原因,通用Windows应用程序只能访问外部驱动器上某些类型的文件 音乐 图画 录像带 您必须在Package.appxmanifest文件中显式声明它

我正在用树莓圆周率写一个应用程序。我想将数据写入连接到其中一个USB端口的外部闪存驱动器。我已经找到了如何在PI中写入SD卡的示例,但是在最终产品中无法访问SD卡

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


有人能给我举一个允许访问外部闪存驱动器的例子吗?

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

  • 音乐
  • 图画
  • 录像带
您必须在Package.appxmanifest文件中显式声明它

  • 音乐图书馆
  • 图片库
  • 视频库
您可能还需要检查可移动存储功能

我认为除了上述三种类型之外,您没有访问通用文件格式的权限,否则您将获得“访问被拒绝”异常

在中查找更多详细信息

声明功能后,可以使用以下代码获取外部存储设备的根文件夹:

var removableDevices = KnownFolders.RemovableDevices;
var externalDrives = await removableDevices.GetFoldersAsync();
var drive0 = externalDrives[0];
然后,您可以使用流方法按照中的代码示例写入文件

如果您想将数据写入通用文件格式,一个解决方法是使用可访问的文件格式(如jpg),并将原始数据写入其中。下面是在Raspberry Pi 2型号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);
            }
        }
    }

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

  • 音乐
  • 图画
  • 录像带
您必须在Package.appxmanifest文件中显式声明它

  • 音乐图书馆
  • 图片库
  • 视频库
您可能还需要检查可移动存储功能

我认为除了上述三种类型之外,您没有访问通用文件格式的权限,否则您将获得“访问被拒绝”异常

在中查找更多详细信息

声明功能后,可以使用以下代码获取外部存储设备的根文件夹:

var removableDevices = KnownFolders.RemovableDevices;
var externalDrives = await removableDevices.GetFoldersAsync();
var drive0 = externalDrives[0];
然后,您可以使用流方法按照中的代码示例写入文件

如果您想将数据写入通用文件格式,一个解决方法是使用可访问的文件格式(如jpg),并将原始数据写入其中。下面是在Raspberry Pi 2型号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);
            }
        }
    }

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

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

一些示例代码将有助于故障排除。一些示例代码将有助于故障排除。这非常有效,谢谢。通过在应用程序清单文件中添加文件关联,我还可以创建一个txt文件。但是它不允许csv文件。我收回csv文件注释。它也适用于该文件类型。非常有效,谢谢。通过在应用程序清单文件中添加文件关联,我还可以创建一个txt文件。但是它不允许csv文件。我收回csv文件注释。它也适用于该文件类型。