Universal Windows RT Storage Helper

  1. namespace LocalStorageSample.Common.Storage  
  2. {  
  3.     public enum Folder  
  4.     {  
  5.         Temporary,  
  6.         Local,  
  7.         Settings  
  8.     }  
  9. }  
  1. namespace LocalStorageSample.Common.Storage  
  2. {  
  3.     using System;  
  4.     using System.Linq;  
  5.     using System.Threading.Tasks;  
  6.   
  7.     using Windows.Storage;  
  8.     using Windows.Storage.Streams;  
  9.   
  10.     public class StorageHelper  
  11.     {  
  12.         public async Task<StorageFolder> GetFolder(Folder folder)  
  13.         {  
  14.             var result = ApplicationData.Current.LocalFolder;  
  15.   
  16.             switch (folder)  
  17.             {  
  18.                 case Folder.Temporary:  
  19.                     result = ApplicationData.Current.TemporaryFolder;  
  20.                     break;  
  21.                 case Folder.Settings:  
  22.                     result = await result.CreateFolderAsync("Settings", CreationCollisionOption.OpenIfExists);  
  23.                     break;  
  24.             }  
  25.   
  26.             return result;  
  27.         }  
  28.   
  29.         public async Task<StorageFile> GetFile(Folder folder, string fileName)  
  30.         {  
  31.             if (string.IsNullOrWhiteSpace(fileName))  
  32.             {  
  33.                 return null;  
  34.             }  
  35.   
  36.             var storageFolder = await this.GetFolder(folder);  
  37.   
  38.             var file = await this.GetFile(storageFolder, fileName);  
  39.   
  40.             return file;  
  41.         }  
  42.   
  43.         public async Task<StorageFile> GetFile(StorageFolder storageFolder, string fileName)  
  44.         {  
  45.             var files = await storageFolder.GetFilesAsync();  
  46.             var file = files.FirstOrDefault(x => x.Name == fileName);  
  47.             return file;  
  48.         }  
  49.   
  50.         public async Task<StorageFile> SaveBytes(StorageFolder folder, string fileName, byte[] contents)  
  51.         {  
  52.             var file = await CreateStorageFile(folder, fileName);  
  53.   
  54.             if (file != null)  
  55.             {  
  56.                 await FileIO.WriteBytesAsync(file, contents);  
  57.             }  
  58.   
  59.             return file;  
  60.         }  
  61.   
  62.         public async Task<StorageFile> SaveText(StorageFolder folder, string fileName, string contents)  
  63.         {  
  64.             var file = await CreateStorageFile(folder, fileName);  
  65.   
  66.             if (file != null)  
  67.             {  
  68.                 await FileIO.WriteTextAsync(file, contents);  
  69.             }  
  70.   
  71.             return file;  
  72.         }  
  73.   
  74.         public async Task<string> GetText(string path)  
  75.         {  
  76.             var file = await StorageFile.GetFileFromPathAsync(path);  
  77.             var contents = await FileIO.ReadTextAsync(file);  
  78.   
  79.             return contents;  
  80.         }  
  81.   
  82.         public async Task<byte[]> GetBytes(string path)  
  83.         {  
  84.             var file = await StorageFile.GetFileFromPathAsync(path);  
  85.   
  86.             using (IRandomAccessStream stream = await file.OpenReadAsync())  
  87.             {  
  88.                 using (var reader = new DataReader(stream.GetInputStreamAt(0)))  
  89.                 {  
  90.                     await reader.LoadAsync((uint)stream.Size);  
  91.                     var contents = new byte[stream.Size];  
  92.                     reader.ReadBytes(contents);  
  93.                     return contents;  
  94.                 }  
  95.             }  
  96.         }  
  97.   
  98.         private static async Task<StorageFile> CreateStorageFile(IStorageFolder folder, string fileName)  
  99.         {  
  100.             StorageFile file = null;  
  101.   
  102.             if (folder == null)  
  103.             {  
  104.                 return null;  
  105.             }  
  106.   
  107.             if (string.IsNullOrWhiteSpace(fileName))  
  108.             {  
  109.                 return null;  
  110.             }  
  111.   
  112.             file = await folder.CreateFileAsync(fileName);  
  113.             return file;  
  114.         }  
  115.     }  
  116. }  
You can extend the Folder enum to add your own custom folder locations.