Windows 10 IoT Applications With UWP C# | Raspberry Pi | Amit Padhiyar | Saatody
How To Get Removable Devices?
Before using this code, You need to enable (check) Removable Storage in Capabilities (in Package.appxmanifest). Warning: Due to async and await keywords, The debugger might not receive some values at breakpoints.
StorageFolder Instance = KnownFolders.RemovableDevices;
This how you can get Removable Devices.
How To Get List Of Removable Devices?
StorageFolder Instance = KnownFolders.RemovableDevices;
IReadOnlyList<StorageFolder> Devices = await Instance.GetFoldersAsync();
How To Get Removable Devices By Array Index?
Get devices using array index like Devices[0] indicate first device for example "C:", Devices[1] indicate second device for example "D:". If index out of range then it will gives you exception.
StorageFolder Instance = KnownFolders.RemovableDevices;
IReadOnlyList<StorageFolder> Devices = await Instance.GetFoldersAsync();
StorageFolder Device = Devices[0];
How To Get All Directories And All Files From Current Path?
StorageFolder Device = Devices[0];
IReadOnlyList<StorageFolder> RootFolders = await Device.GetFoldersAsync();
IReadOnlyList<StorageFile> RootFiles = await Device.GetFilesAsync();
StorageFolder RootFolder = await Device.GetFolderAsync("Folder Name");
StorageFile RootFile = await Device.GetFileAsync("File Name.txt");
Here is four methods to get all directories and all files. The method GetFoldersAsync() will give all folders from current path. As it is, The method GetFilesAsync() will give all files from current path. The methods GetFolderAsync() and GetFileAsync() will give folder and file by name.
Some Files Not Retrieve Using The GetFilesAsync()
Users commonly get exception while they try to retrieve file from removeable device. For example: Some users got System.UnauthorizedAccessException: 'Access is denied. ' exception. So first please check... Is file exists or not? Second, Enable (check) Removable Storage in Capabilities (in Package.appxmanifest).
After done these both steps, You can retrieve files using GetFilesAsync() method. In some case, file is also exists but still not show in list then you need to go "Package.appxmanifest", in tab of "Declarations", Select "File Type Associations" and press "Add" button. Next, In properties section write extension name ("Name:") in small letter (for example: "xyz_file"). then write extension ("File type:") in small letter with dot symbol (for example: ".xyz"). Now save and run program. I hope it should run successfully.
In some case, You are trying to retrieve file using GetFileAsync("FileName.ext") and get same exception of "Access is denied" but file is exists. Then try just above step of extension Declarations.
Comments
Post a Comment