Search code examples
c#uwpraspberry-piiotwindows-10-iot-core

UWP/C#/IoT/RPi: Access USB Device (Stick) from Win IoT on RPi AND Copy to KnownFolders


Here is my challenge:

  • 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.

Some things I tried:

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?


Solution

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

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

    Destination folder: KnownFolders.DocumentsLibrary root folder.

    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);
            }
    
        }
    

    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>