A tweet by @martinsuchan earlier reminded me of an issue I faced porting some code from Windows Phone to Windows Store. Moving from the Isolated Storage API to the new Windows.Storage namespace there was nothing built-in to determine if a file/folder exists prior to Windows 8.1. This meant the only supported option was to try and access the file and catch an exception. This is in itself horrible but even worse if you’re trying to use the same code on Windows Phone and Windows 8, because these exceptions have an even worse impact on the phone. My solution was to create an extension method for StorageFolder which would use the Isolated Storage API under the hood to call FileExists() or DirectoryExists() on Windows Phone and only the try/catch method when compiled on Windows 8. I had this code sitting around for a while and following 8.1 I reworked the code to add a TryGetItemAsync extension method for Windows Phone and I didn’t need the Windows 8 version any more. This functionality is packaged up in “Charming Storage” which is available on NuGet:-


Add the package, insert:

using InTheHand.Storage;

At the top of your code file and then you can use the extension method in exactly the same way as the Windows 8.1 method. Checking for file existence is as simple as:-

if(await Windows.Storage.ApplicationData.Current.LocalFolder.TryGetItemAsync("cheese") != null)

The Charming Storage library also adds a local settings API which is code compatible with Windows 8 and stores settings in the package’s local storage. This offers a replacement to IsolatedStorageSettings and gives you the same code as Windows Store (just the namespace is different).

About the Author Peter Freeman Foot

Microsoft Windows Development MVP