42884

Metro Style App HTML/JS writing local files

Question:

I've built an app for Windows Store that writes to local files using Windows.Storage.FileIO (createFileAsync, readTextAsync and writeTextAsync).

My plan is to use the file as a simple database (the data is simply JSON formatted user information). Is this a bad idea?

Are the files written this way really persistent (or are they deleted when the app is updated)? Can the user themselves delete them (for example through deleting cookies/temp internet files or the likes)?

I'm just wondering how files written to the disk locally by apps are treated by the "Metro" environment...

Answer1:

The question of whether it's a "bad idea" is quite subjective, so probably not the right venue for that.

However, in terms of how Windows Store apps treat files you store, if you create and/or edit files using Windows.Storage.ApplicationData.current.localFolder, these files will be available until the user uninstalls the app. If you push an update of your app to the store, and the user downloads and installs the update, the files will remain on the user's machine, unless you explicitly modify them programmatically.

In two apps that I've built, I leverage the localFolder to store JSON representations of the app's data so that I can minimize startup time, and refresh the data in the background once the app has started up and rendered its initial UI to the user.

Here's what my code (which could probably be optimized a bit) looks like:

<pre class="lang-js prettyprint-override">appData.current.localFolder.createFileAsync("leaderboard.txt", Windows.Storage.CreationCollisionOption.openIfExists) .then(function (file) { leaderboardFile = file; return Windows.Storage.FileIO.readTextAsync(file) }) .then(function (fileContents) { cachedLeaderboard = fileContents; if (!cachedLeaderboard || isRefresh) { return WinJS.xhr(xhrOptions) } else { cachedLeaderboard = JSON.parse(cachedLeaderboard); List = new WinJS.Binding.List(cachedLeaderboard); completed(List); } }) .then(function (result) { if (result) { var items = JSON.parse(result.responseText).d; localLeaderboard = items; return Windows.Storage.FileIO.writeTextAsync(leaderboardFile, JSON.stringify(localLeaderboard)) } }) .then(function () { if (!localLeaderboard) localLeaderboard = cachedLeaderboard; List = new WinJS.Binding.List(localLeaderboard); completed(List); })

Basically, the code will check whether the leaderboard.txt file exists (or if the call is a refresh of the data), and if not, make an XHR call to get the data, then create a binding list and pass it back to the caller wrapped in a promise (the promise code isn't shown). If the file exists, and the request isn't a refresh, I just parse the text file and create the binding list from that.

Hope that helps!

Recommend

  • Why Would A .NET 4.0 WPF App Have Much Better UI Performance On VMWare Guest Than Host?
  • jQuery(“#id”) or jQuery(document.getElementById(“id”))? [closed]
  • What advantage is there to overriding the == operator in an ORM?
  • need to fix retina sprite css issue
  • Creating desktop applications for Windows RT?
  • How to convert a Java object to an XML string, and conversely convert XML to a Java object?
  • Is there no built in way to get a table's schema in ADO.NET?
  • Error compiling OpenSSL for Win32
  • Subclass HashSet so that it always uses a certain IEqualityComparer when used in another set
  • Determining signed state for HDF5 variables in NetCDF
  • Session management in GWT client side
  • IP and domain create different session
  • How does Skobbler prioritize which POIs to show?
  • Unable to install breakpoint in Eclipse: Absent Line Number Information
  • Selenium and Google - How do you use cookies?
  • Sed: Using a,c or i in a one-liner
  • Handling exceptions in a class library enveloping a device driver
  • How to make a user wait with Laravel
  • Unable to gem install nokogiri
  • GRPC and types import error in App Engine Datastore
  • How to use SBT with multiple sub project web applications?
  • Zend Framework bassed projects
  • Are there any side effects from calling SQLAlchemy flush() within code?
  • Problems with toDataURL HTML5 other ways to get canvas data?
  • NSIS decompiler
  • How to skip require in ruby?
  • User messaging system
  • How to 'create temp table as select' in Slick?
  • How load cv2.KeyPoint and Descriptors correctly on OpenCV 3 with Python 3?
  • Saving image to sd with current date and time in name doesn't work
  • ASP.NET MVC Application won't update some controllers
  • Yii2: Config params vs. const/define
  • Ajax Loaded meta Tags
  • Paperclip, set path outside of rails root folder
  • Xamarin Forms - UWP Fonts
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • 0x202A in filename: Why?
  • Arrays break string types in Julia
  • How can I remove ASP.NET Designer.cs files?
  • Programmatically clearing map cache