Search code examples
c#windowsuwpwindowsiotwindows-iot-core-10

UWP - Windows IOT - Getting files from a specific folder named "ad_runner" located in a USB drive


I am having unhandled error show up when I am trying to access a file from a folder named "ad_runner" Which is located a USB drive. What am I doing wrong?

        async void AccessTheWebAsync()
    {
        StorageFolder remd = await KnownFolders.RemovableDevices.GetFolderAsync("ad_runner");
        IReadOnlyList<StorageFile> fileList = await remd.GetFilesAsync();
        foreach (StorageFile file in fileList)
        {
            dir_lbl.Text = dir_lbl.Text + "\r\n" + file.Name;
        }
    }

Solution

  • If you try to call await KnownFolders.RemovableDevices.GetFoldersAsync(); you can see that this first call don't return the folders inside removable devices but the removable devices themself. So, I modified a little your code to first retrieve all connected removable devices and, after this, checking for inside folder named "ad_runner":

    async void AccessTheWebAsync()
        {
            //Maybe you want to do this inside a try catch and verify that at least 1 device is connected before you go on....
            var folderList = await KnownFolders.RemovableDevices.GetFoldersAsync();
    
            foreach (var device in await KnownFolders.RemovableDevices.GetFoldersAsync())
            {
                try
                {
                    StorageFolder remd = await device.GetFolderAsync("ad_runner");
                    IReadOnlyList<StorageFile> fileList = await remd.GetFilesAsync();
                    foreach (StorageFile file in fileList)
                    {
                        //Your logic here....
                       dir_lbl.Text = dir_lbl.Text + "\r\n" + file.Name;
                    }
                }
                catch (Exception)
                {
    
                }
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    rightGridWrapper.Children.Add(new TextBlock { Text = $"DeviceName: {device.Name}" });
                });
            }
    }