File Access using Windows.Storage.Pickers.FileOpenPicker and Windows.Storage.StorageFile RRS feed

  • Question

  • Hi,

    I have the following use case: I create a FileOpenPicker (starting at KnownFolders.documentsLibrary). With this picker I can pick a file anywhere I like (e.g. C:\somefile.txt) and get a StorageFile for it.

    However, when I store the file's path and try to recreate a Windows.Storage.StorageFile later using StorageFile.getFileFromPathAsync, I am not allowed to do that, getting the following error:

    WinRT information: Cannot access the specified file or folder (C:\somefile.txt). The item was not found in any of the allowed locations (including application data folders, folders that are accessible via capabilities, and persisted items in the StorageApplicationPermissions lists). Verify that the file is not marked with the system or hidden file properties.

    Why am I allowed to have a StorageFile pointing to that path in one case, but not in the other?

    Friday, February 3, 2012 10:49 AM


All replies

  • Hi G,

    That is by design.  You could use a FilePicker to allow the user to store it somewhere else later (and they could choose the same location), or you could keep the file open that you got from the picker and write to it.  You cannot use a stored path for writing files. 


    Jeff Sanders (MSFT)
    Friday, February 3, 2012 1:59 PM
  • Sorry to correct you, Jeff :)

    There is a facility in WinRT for keeping previously-picked storage items for future use: see Windows.Storage.AccessCache (http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.accesscache.aspx). Some of this is used in the File Access sample (http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597) which is a starting point.

    In other words, you can't just directly save a file path to get back to it later. Windows needs to save the appropriate internal permissions along with the path, which is what the AccessCache does.



    Friday, February 3, 2012 10:53 PM
  • Thanks Kraig for your correction. I am sure Jeff does not mind ;)

    Its good to know that there is a way to get previously picked files in a later program use or otherwise how would you implement a "Most recent files" feature to let the user re-open previously picked/opened files?

    Sunday, February 5, 2012 2:53 PM
  • Excellent!  Thanks Kraig!
    Jeff Sanders (MSFT)
    Monday, February 6, 2012 1:11 PM