I've two files with the same name in KnownFolders.VideosLibrary, in this case I cannot access file by its Name hence it will return only the first one. Therefore is there any other way to get the file other that parsing all files in folder?
// this return two files
var files = (await KnownFolders.VideosLibrary.GetFilesAsync()).Where(x => x.Name == "test.txt").ToArray();
// with this I can get only one file
StorageFile file = await KnownFolders.VideosLibrary.GetFileAsync("test.txt");
// of course I can parse it with query, but I would like to avoid it
// StorageFile myFile = (await KnownFolders.VideosLibrary.GetFilesAsync()).FirstOrDefault(x => x.FolderRelativeId == "something");
I'm aware of FutureAccessList, but it can hold only up to 1000 files, what is not enough for me.
Some clarification after request:
For example lets consider that app run on phone with SD card. I've one file in Videos in phone's memory with name test.txt
, the file with the same name exists also on SD card in Videos folder.
In this situation when you call the first line in code above, you will get two files, to differentiate them system provides FolderRelativeId, so files with the same name can exist in one 'location'. If you take a look at the full path of each folder one will likely have C:\Viedos\test.txt
and the second D:\Videos\test.txt
.
Now user on the first run picked a file with FilePicker and I've remembered its path for example D:\Videos\test.txt
. On the second run of the app I would like to have access to this file by using its path (or other method apart from limited FutureAccessList). In the past I used to do it by StorageFile.GetFileFromPathAsync(path);
- by it seems that it starts throwing UnauthorizedAccessException in W10.
Finding the Path with the Shift Key 1 Open Windows Explorer and find your file. 2 Press Shift and right-click with your mouse. 3 Your pop up menu will have several more options. 4 Pin 5 Select Copy as Path 6 Paste where needed.
Press and hold the Shift Key and right-click on a file or folder you want to copy the full path of. Now, click the Copy as path option from the Windows context menu. That’s it! The full location path of the selected file or folder will be copied to the clipboard.
On older versions of Windows, open the Start Menu and type “ folder options ” and select Folder Options to open it. When the File Explorer Options window pops up, click on the View tab. Look for Display the full path in the title bar options under Files and Folders and enable it. Finally, click on Apply.
Find the path next to “Location. ” It’s near the center of the window. Navigate to the folder that contains the file. For example, if the file is on your desktop, go to the desktop. Press ⊞ Win + R.
You can get the instance of IStorageFile
by this.
// If your have a StorageFile objece file then
//fileUri = new Uri(file.Path);
string uri = fileUri.LocalPath;
int end = uri.LastIndexOf('\\');
uri = uri.Substring(0, end + 1);
string name = Path.GetFileName(fileUri.LocalPath);
var folder = await StorageFolder.GetFolderFromPathAsync(uri);
IStorageFile file = await folder.GetFileAsync(name);
// Do something with file object
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With