Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any right way to get a file by its Path?

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.

like image 773
Romasz Avatar asked Dec 06 '15 07:12

Romasz


People also ask

How do I find the path of a file?

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.

How do I copy the full path of a file?

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.

How to display the full path in Windows 10?

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.

How do I find the location of a file in Windows?

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.


1 Answers

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
like image 137
Aman Sharma Avatar answered Oct 18 '22 17:10

Aman Sharma