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

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

问题描述

这是我的挑战:

  • Windows IoT on Raspi,C# UWP-App
  • 在启动或点击...
    • 枚举已连接的 USB 设备
    • 选择 U 盘
    • 枚举文件和文件夹
    • 如果找到某个文件夹名称,将文件夹原样复制到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?

    推荐答案

    有一个我在 Raspberry Pi 3 和 Windows 10 IoT Core 上测试的示例.这个对我有用.

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

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

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

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

    Destination folder: KnownFolders.DocumentsLibrary root folder.

    以下代码完成将 test 文件夹中的文件复制到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 + ":
    ");
    
            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 + ":
    ");
    
            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 + "
    ");
                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 设备(棒)并复制到已知文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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