4

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.

VSZM
  • 1,341
  • 2
  • 17
  • 31
  • I believe ms-appdata:///local/my_folder equals "C:\Data\Users\DefApps\AppData\{2F102375-2740-441C-BF2F-808608F47DA1}\LocalState\my_folder" – K Mehta Jan 21 '14 at 00:05
  • No. I have checked with WP Power Tools, that my_folder exists in my application, but it throws a FileNotFound exception when I try to get the StorageFolder for your path, however it works just fine with mine. – VSZM Jan 21 '14 at 01:22
  • What do you mean, you're trying to get the `StorageFolder` for what path? Could you edit the question to include the relevant code lines? – WiredPrairie Jan 21 '14 at 01:23

1 Answers1

4

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.