Access a StorageFolder with ms-appdata

4.4k views Asked by At

I have an app where I have to store and use the absolute paths of folders and files. I have a very simple problem. When I store the path of a folder like this: "ms-appdata:///local/my_folder" and try to get a StorageFolder from this path, it throws a FileNotFoundException. Why is this exception thrown?

AFAIK "ms-appdata:///local/my_folder" equals "C:\Data\Users\DefApps\AppData\{2F102375-2740-441C-BF2F-808608F47DA1}\Local\my_folder". The latter string is accepted by the GetFolderFromPathAsync static method of StorageFolder. How can I create the latter Uri from the former and vice versa?

Edit: clarified question.

1

There are 1 answers

0
Kraig Brockschmidt - MSFT On

The static method GetFolderFromPathAsync works with :\ syntax. ms-appdata:/// is not a pathname, but a URI scheme, meant to work with the Windows.Storage.StorageFile.GetFileFromApplicationUriAsync method. Unfortunately there isn't the equivalent method for folders.

This leaves you with a couple of options. One is that you could store a simple reference file in that folder, use GetFileFromApplicationUriAsync to get its StorageFile, and then look StorageFile.GetParentAsync to get the StorageFolder.

The other option is to just get your local folder from Windows.Storage.ApplicationData.LocalFolder and then do GetFolderAsync on the relative part of the path. And if you must reconstruct the file path, then get the LocalFolder, the append your relative folder path to its Path property. This way you won't ever rely on the exact user's path to their appdata.