【问题标题】:UWP/C#/IoT/RPi: Access USB Device (Stick) from Win IoT on RPi AND Copy to KnownFoldersUWP/C#/IoT/RPi:从 RPi 上的 Win IoT 访问 USB 设备(棒)并复制到 KnownFolders
【发布时间】:2019-03-08 01:17:09
【问题描述】:

这是我的挑战:

  • Raspi 上的 Windows IoT,C# UWP-App
  • 在启动或单击时...
    • 枚举连接的 USB 设备
    • 选择 U 盘
    • 枚举棒上的字段和文件夹
    • 如果找到某个文件夹名称,将文件夹原样复制到 KnownFolders.Documents

令人惊讶的是,网络上有许多半解决方案,但没有一个几乎不起作用。

我尝试过的一些事情:

var removableDeviceList = await KnownFolders.RemovableDevices.GetFoldersAsync();
            if (removableDeviceList.Count > 0)
            {
                StorageFolder targetDevice = removableDeviceList.FirstOrDefault();
                ListBox.Items.Add(targetDevice.Name);
}

到目前为止工作,但后来卡住了。

是的,在清单中激活了图片库、文件定义、可移动设备等功能。想不到这个基础的东西真的是这么难解决?

【问题讨论】:

    标签: c# uwp raspberry-pi iot windows-10-iot-core


    【解决方案1】:

    我在 Raspberry Pi 3 上使用 Windows 10 IoT Core 测试了一个示例。这个对我有用。

    源文件夹:U盘根目录下的test文件夹(E:\test)。共有三个文件:hello.txt、welcome1.png、welcome3.jpg。

    目标文件夹:KnownFolders.DocumentsLibrary 根文件夹。

    以下代码将 test 文件夹中的文件完整复制到 KnownFolders.DocumentsLibrary 根文件夹。

       public async void USBDriveCopyFolder()
        {
            var targetFolderName = "test";
    
            var removableDevice = (await KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault();
            if (null == removableDevice)
            {
                System.Diagnostics.Debug.WriteLine("removableDevice is null !");
                return;
            }
            System.Diagnostics.Debug.WriteLine(removableDevice.Name + ":\n");
    
            var sourceFolder = await removableDevice.GetFolderAsync(targetFolderName);
            if (null == sourceFolder)
            {
                System.Diagnostics.Debug.WriteLine(targetFolderName + " folder is not found !");
                return;
            }
            System.Diagnostics.Debug.WriteLine(sourceFolder.Name + ":\n");
    
            var destFodler = KnownFolders.DocumentsLibrary;
            if (null == destFodler)
            {
                System.Diagnostics.Debug.WriteLine("KnownFolders.DocumentsLibrary folder get failed !");
                return;
            }
    
            var files = await sourceFolder.GetFilesAsync();
    
            foreach (var file in files)
            {
                System.Diagnostics.Debug.WriteLine(file.Name + "\n");
                await file.CopyAsync(destFodler);
            }
    
        }
    

    package.appxmanifest 中的设备功能:

      <Applications>
    
        ...
        ...
    
        <Application>
          <Extensions>
            <uap:Extension Category="windows.fileTypeAssociation">
              <uap:FileTypeAssociation Name="txt">
                <uap:SupportedFileTypes>
                  <uap:FileType>.txt</uap:FileType>
                </uap:SupportedFileTypes>
              </uap:FileTypeAssociation>
            </uap:Extension>
            <uap:Extension Category="windows.fileTypeAssociation">
              <uap:FileTypeAssociation Name="jpg">
                <uap:SupportedFileTypes>
                  <uap:FileType>.jpg</uap:FileType>
                </uap:SupportedFileTypes>
              </uap:FileTypeAssociation>
            </uap:Extension>
            <uap:Extension Category="windows.fileTypeAssociation">
              <uap:FileTypeAssociation Name="png">
                <uap:SupportedFileTypes>
                  <uap:FileType>.png</uap:FileType>
                </uap:SupportedFileTypes>
              </uap:FileTypeAssociation>
            </uap:Extension>
          </Extensions>
        </Application>
      </Applications>
      <Capabilities>
        <uap:Capability Name="picturesLibrary" />
        <uap:Capability Name="removableStorage" />
        <uap:Capability Name="documentsLibrary" />
      </Capabilities>
    

    【讨论】:

    • 像魅力一样工作。
    • 在这种特定情况下不要忘记将 添加到如上所示的功能中(不能在 appxmanifest 的 UI 版本中直接单击,使用正确点击和“源代码”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多