-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathDesktopFileSystem.cs
More file actions
97 lines (88 loc) · 3.7 KB
/
DesktopFileSystem.cs
File metadata and controls
97 lines (88 loc) · 3.7 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PCLStorage
{
/// <summary>
/// Implementation of <see cref="IFileSystem"/> over classic .NET file I/O APIs
/// </summary>
public class DesktopFileSystem : IFileSystem
{
/// <summary>
/// A folder representing storage which is local to the current device
/// </summary>
public IFolder LocalStorage
{
get
{
// SpecialFolder.LocalApplicationData is not app-specific, so use the Windows Forms API to get the app data path
//var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
#if ANDROID
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
#elif IOS
NSUrl[] urls = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomain.User);
if (urls == null || urls.Count() == 0) {
throw new Exception("Failed to find the folder; this should never happen.");
}
return urls[0].Path;
#else
var localAppData = System.Windows.Forms.Application.LocalUserAppDataPath;
#endif
return new FileSystemFolder(localAppData);
}
}
/// <summary>
/// A folder representing storage which may be synced with other devices for the same user
/// </summary>
public IFolder RoamingStorage
{
get
{
#if ANDROID || IOS
return null;
#else
// SpecialFolder.ApplicationData is not app-specific, so use the Windows Forms API to get the app data path
//var roamingAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var roamingAppData = System.Windows.Forms.Application.UserAppDataPath;
return new FileSystemFolder(roamingAppData);
#endif
}
}
/// <summary>
/// Gets a file, given its path. Returns null if the file does not exist.
/// </summary>
/// <param name="path">The path to a file, as returned from the <see cref="IFile.Path"/> property.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A file for the given path, or null if it does not exist.</returns>
public async Task<IFile> GetFileFromPathAsync(string path, CancellationToken cancellationToken)
{
Requires.NotNullOrEmpty(path, "path");
await AwaitExtensions.SwitchOffMainThreadAsync(cancellationToken);
if (File.Exists(path))
{
return new FileSystemFile(path);
}
return null;
}
/// <summary>
/// Gets a folder, given its path. Returns null if the folder does not exist.
/// </summary>
/// <param name="path">The path to a folder, as returned from the <see cref="IFolder.Path"/> property.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A folder for the specified path, or null if it does not exist.</returns>
public async Task<IFolder> GetFolderFromPathAsync(string path, CancellationToken cancellationToken)
{
Requires.NotNullOrEmpty(path, "path");
await AwaitExtensions.SwitchOffMainThreadAsync(cancellationToken);
if (Directory.Exists(path))
{
return new FileSystemFolder(path, true);
}
return null;
}
}
}