1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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);
}
}
}
|