diff options
author | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 13:02:25 -0700 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 16:13:41 -0700 |
commit | 17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch) | |
tree | b5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Platform.WinRT/WindowsIsolatedStorage.cs | |
download | xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2 xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip |
Initial import
Diffstat (limited to 'Xamarin.Forms.Platform.WinRT/WindowsIsolatedStorage.cs')
-rw-r--r-- | Xamarin.Forms.Platform.WinRT/WindowsIsolatedStorage.cs | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.WinRT/WindowsIsolatedStorage.cs b/Xamarin.Forms.Platform.WinRT/WindowsIsolatedStorage.cs new file mode 100644 index 00000000..a6e48fea --- /dev/null +++ b/Xamarin.Forms.Platform.WinRT/WindowsIsolatedStorage.cs @@ -0,0 +1,119 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Windows.Storage; +using Windows.Storage.FileProperties; +using Windows.Storage.Streams; + +#if WINDOWS_UWP + +namespace Xamarin.Forms.Platform.UWP +#else + +namespace Xamarin.Forms.Platform.WinRT +#endif +{ + internal class WindowsIsolatedStorage : IIsolatedStorageFile + { + StorageFolder _folder; + + public WindowsIsolatedStorage(StorageFolder folder) + { + if (folder == null) + throw new ArgumentNullException("folder"); + + _folder = folder; + } + + public Task CreateDirectoryAsync(string path) + { + return _folder.CreateFolderAsync(path).AsTask(); + } + + public async Task<bool> GetDirectoryExistsAsync(string path) + { + try + { + await _folder.GetFolderAsync(path).AsTask().ConfigureAwait(false); + return true; + } + catch (FileNotFoundException) + { + return false; + } + } + + public async Task<bool> GetFileExistsAsync(string path) + { + try + { + await _folder.GetFileAsync(path).AsTask().ConfigureAwait(false); + return true; + } + catch (FileNotFoundException) + { + return false; + } + } + + public async Task<DateTimeOffset> GetLastWriteTimeAsync(string path) + { + StorageFile file = await _folder.GetFileAsync(path).AsTask().ConfigureAwait(false); + BasicProperties properties = await file.GetBasicPropertiesAsync().AsTask().ConfigureAwait(false); + return properties.DateModified; + } + + public async Task<Stream> OpenFileAsync(string path, FileMode mode, FileAccess access) + { + StorageFile file; + + switch (mode) + { + case FileMode.CreateNew: + file = await _folder.CreateFileAsync(path, CreationCollisionOption.FailIfExists).AsTask().ConfigureAwait(false); + break; + + case FileMode.Create: + case FileMode.Truncate: // TODO See if ReplaceExisting already truncates + file = await _folder.CreateFileAsync(path, CreationCollisionOption.ReplaceExisting).AsTask().ConfigureAwait(false); + break; + + case FileMode.OpenOrCreate: + case FileMode.Append: + file = await _folder.CreateFileAsync(path, CreationCollisionOption.OpenIfExists).AsTask().ConfigureAwait(false); + break; + + case FileMode.Open: + file = await _folder.GetFileAsync(path); + break; + + default: + throw new ArgumentException("mode was an invalid FileMode", "mode"); + } + + switch (access) + { + case FileAccess.Read: + return await file.OpenStreamForReadAsync().ConfigureAwait(false); + case FileAccess.Write: + Stream stream = await file.OpenStreamForWriteAsync().ConfigureAwait(false); + if (mode == FileMode.Append) + stream.Position = stream.Length; + + return stream; + + case FileAccess.ReadWrite: + IRandomAccessStream randStream = await file.OpenAsync(FileAccessMode.ReadWrite).AsTask().ConfigureAwait(false); + return randStream.AsStream(); + + default: + throw new ArgumentException("access was an invalid FileAccess", "access"); + } + } + + public Task<Stream> OpenFileAsync(string path, FileMode mode, FileAccess access, FileShare share) + { + return OpenFileAsync(path, mode, access); + } + } +}
\ No newline at end of file |