Copy files to USB using C# Windows 10 iot core

Viewed 304

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.

1 Answers

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.

Related