3

I am trying to copy .csv files created in the local folder in Windows 10 iot core. I have tried few ways but no luck. My latest code is as follows:

string aqs = UsbDevice.GetDeviceSelector(0x045E, 0x0611);

        var myDevices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(aqs);
        UsbDevice usbDevice;

        try
        {
            if(myDevices == null)
            {
                return;
            }
            usbDevice = await UsbDevice.FromIdAsync(myDevices[0].Id);


            StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFolder sourcef = await localFolder.CreateFolderAsync("My Data", CreationCollisionOption.OpenIfExists);
            IReadOnlyList<StorageFile> filel = await sourcef.GetFilesAsync();

            StorageFolder removableDevices = KnownFolders.RemovableDevices;
            //var externalDrives = await removableDevices.GetFoldersAsync();
            //var drive0 = externalDrives[0];

            //var destFolder = await removableDevices.CreateFolderAsync("My Data", CreationCollisionOption.GenerateUniqueName);

            foreach (StorageFile file in filel)
            {
                await file.CopyAsync(removableDevices);
            }
    }

In the above i get an exception on:

usbDevice = await UsbDevice.FromIdAsync(myDevices[0].Id);

'myDevices[0].Id' threw an exception of type 'System.ArgumentOutOfRangeException'

I have tried checking if this is null and it is not null.

The aim of this is to basically copy few text files from Local Folder to the USB drive.

Utkarsh
  • 65
  • 8
  • The error doesn't say it's null. It says it's empty. So, I guess your device has not been found. Maybe try and change `if(myDevices == null)` to `if(myDevices is null || !myDevices.Any())` – Fildor Sep 11 '20 at 06:27
  • If I connect the device before starting the debugging, the windows 10 iot core default app shows it. So device is recognized by windows. However, changing the code as above might still not solve the problem. Thanks anyway! – Utkarsh Sep 11 '20 at 06:32
  • 1
    does this help https://learn.microsoft.com/en-us/uwp/api/windows.devices.usb.usbdevice?view=winrt-19041 – Seabizkit Sep 11 '20 at 06:32
  • At least above Exception won't be the cause. So you'll be one step further :) – Fildor Sep 11 '20 at 06:34
  • 1
    BTW: By "your device has not been found" I meant: Either literally your device has not been found _or_ maybe the device is known by a different selector than you expect? – Fildor Sep 11 '20 at 06:36
  • @Fildor yes I have tried your suggestion and the USB is not found. – Utkarsh Sep 12 '20 at 07:16
  • @Seabizkit. thanks for the suggestion. I am getting to "Device not found" part there. – Utkarsh Sep 12 '20 at 07:17

1 Answers1

2

The usb storage can not be found by using UsbDevice.GetDeviceSelector, the methods in Windows.Devices.Usb namespace are used for valid WinUSB device which has a compatible id of USB\MS_COMP_WINUSB, but a usb storage will not include compatible id.In fact, KnownFolders.RemovableDevices is enough to access the device.

Michael Xu
  • 4,382
  • 1
  • 8
  • 16
  • Thanks Michael. After going through your comment, my final code consists of KnownFolders.RemovableDevices and yes the system is able to get the USB device (as checked in the debugger). However, await file.CopyAsync(removableDevices); is throwing an exception. I have also included "usb" device capability. Please note that file is not empty in the list. – Utkarsh Sep 14 '20 at 06:44
  • The exception says: "Parameter is incorrect \r\n". I am trying to copy .csv files here. – Utkarsh Sep 14 '20 at 07:10
  • Please refer to the code that Rita provided in this link(https://stackoverflow.com/questions/52616357/uwp-c-iot-rpi-access-usb-device-stick-from-win-iot-on-rpi-and-copy-to-knownf), hope it is helpful for you. – Michael Xu Sep 14 '20 at 08:48