UWP/C#/IoT/RPi:从RPi上的Win IoT访问USB设备(Stick)并将其复制到KnownFolders [英] UWP/C#/IoT/RPi: Access USB Device (Stick) from Win IoT on RPi AND Copy to KnownFolders

查看:85
本文介绍了UWP/C#/IoT/RPi:从RPi上的Win IoT访问USB设备(Stick)并将其复制到KnownFolders的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的挑战:

  • Raspi上的Windows IoT,C#UWP-App
  • 在启动或快打...
    • 枚举连接的USB设备
    • 选择USB记忆棒
    • 枚举棍子上的字段和文件夹
    • 如果找到某个文件夹名称,则将文件夹原样复制到KnownFolders.Documents
    • Windows IoT on Raspi, C# UWP-App
    • On Boot or klick...
      • Enumerate connected USB devices
      • Select the USB Stick
      • Enumerate Fiels and folders on the stick
      • If a certain folder name is found, copy folder as-is to KnownFolders.Documents

      令人惊讶的是,网络上有很多解决方案,但几乎没有一个可行.

      It is astonishing that there are many half solutions on the web but none of them hardly works.

      我尝试过的一些事情:

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

      目前为止工作正常,但后来停滞了.

      Works so far but stuck afterwards.

      是的,清单中激活了图片库,文件定义,可移动设备等功能.我不敢相信这个基本的事情真的很难解决吗?

      And yes, functions like Picture library, file definitions, removable devices are activated in the manifest. I can't believe that this basic thing is really such a difficult task to solve?

      推荐答案

      有一个示例我在带有Windows 10 IoT核心版的Raspberry Pi 3上进行了测试.这个对我有用.

      There is an example I test on Raspberry Pi 3 with Windows 10 IoT Core. It works for me.

      源文件夹:USB驱动器根目录中的测试文件夹(E:\ test).一共有三个文件:hello.txt,welcome1.png,welcome3.jpg.

      Source folder: test folder in USB drive root(E:\test). There are three files: hello.txt, welcome1.png, welcome3.jpg .

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

      Destination folder: KnownFolders.DocumentsLibrary root folder.

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

      The following code complete copy files in test folder to KnownFolders.DocumentsLibrary root folder.

         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中的设备功能:

      Device capabilities in 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>
      

      这篇关于UWP/C#/IoT/RPi:从RPi上的Win IoT访问USB设备(Stick)并将其复制到KnownFolders的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆