locked
How to save file path and name. RRS feed

  • Question

  • I have an app I am almost complete and I simply want to reload the file the user was working on when he quit the app. My app has it's own file format and allows the user to load and save with the file picker classes. I just can't figure out how to persist the file name and folder location so I can automatically recall the file he was working on when he starts the app. Or optionally supply a list of 'recent files' he was working on.
    Monday, June 2, 2014 10:23 AM

Answers

All replies

  • You could use the isolated storage to store metadata about your user's data (e.g. last opened files etc.). Isolated storage is used to store a small amount of app-specific data per user. Here is an example on how to access the isolated storage:

    IsolatedStorageFile Storage = IsolatedStorageFile.GetUserStoreForApplication(); 
    
    Storage.CreateDirectory("MySettings"); 
    
    StreamWriter Writer = new StreamWriter(new IsolatedStorageFileStream("MySettings\\LastOpenedFiles.txt", FileMode.OpenOrCreate, Storage)); 
    
    Writer.WriteLine(filename); 
    Writer.Close();

    Here is an open source app that allows you to inspect the isolated storage: Isolated Storage Spy 

    You can also use another kind of database to store you app-data/settings. E.g. the SQLite is supported on Windows Phone: SQLite for Windows Phone

    Hope that helps...


    Monday, June 2, 2014 12:58 PM
  • Also don't forget to add the storage file to the AccessCache, so your app will have permission to load it again without asking the user for the file...

    Darin R.

    Monday, June 2, 2014 1:16 PM
  • Robin's code snippet is for Windows Phone and isn't valid for Windows Store apps.

    As Darin suggests, you don't want to use paths for this. Your app doesn't have access to the filepath returned from the picker and must access it via the returned StorageFile. You can persist the StorageFile via the AccessCache classes. See How to track recently used files and folders and http://blogs.msdn.com/b/wsdevsol/archive/2012/12/05/stray-from-the-path-stick-to-the-storagefile.aspx

    • Marked as answer by KeatsP Monday, June 2, 2014 4:30 PM
    Monday, June 2, 2014 2:58 PM
    Moderator
  • This is exactly what I needed.  Thanks for the great links!
    Monday, June 2, 2014 4:30 PM
  • Got everything working great with only a couple issues that I managed to overcome. 

    This line from MSDN;

    String mruFirstToken = StorageApplicationPermissions.MostRecentlyUsedList.Entries.First.Token;

    I had to change to;

    String mruFirstToken = StorageApplicationPermissions.MostRecentlyUsedList.Entries.First().Token;

    And I also had to surround that same line with a try/catch otherwise it would crash my app when the MRU was empty.  Even comparing it against null had no effect.

    The last question I have on this thread would be whether the MRU is local to each app's sandbox or am I going to end up sorting through other app's token's to find my own?

    Tuesday, June 3, 2014 12:50 AM
  • The AccessCache is specific to your app. You won't need to worry about crossing permissions with other apps.
    Tuesday, June 3, 2014 12:58 AM
    Moderator