-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Environment.Android.cs
99 lines (80 loc) · 3.14 KB
/
Environment.Android.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.IO;
using System.Threading;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace System
{
public static partial class Environment
{
private static Dictionary<SpecialFolder, string>? s_specialFolders;
private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOption _ /*option*/)
{
if (s_specialFolders == null)
{
Interlocked.CompareExchange(ref s_specialFolders, new Dictionary<SpecialFolder, string>(), null);
}
string? path;
lock (s_specialFolders)
{
if (!s_specialFolders.TryGetValue(folder, out path))
{
path = GetSpecialFolder(folder) ?? string.Empty;
s_specialFolders[folder] = path;
}
}
return path;
}
private static string? GetSpecialFolder(SpecialFolder folder)
{
string? home = null;
try
{
home = PersistedFiles.GetHomeDirectory();
}
catch (Exception exc)
{
Debug.Fail($"Unable to get home directory: {exc}");
}
// Fall back to '/' when we can't determine the home directory.
// This location isn't writable by non-root users which provides some safeguard
// that the application doesn't write data which is meant to be private.
if (string.IsNullOrEmpty(home))
{
home = "/";
}
switch (folder)
{
case SpecialFolder.LocalApplicationData:
return home;
case SpecialFolder.ApplicationData:
return Path.Combine(home, ".config");
case SpecialFolder.Desktop:
case SpecialFolder.DesktopDirectory:
return Path.Combine(home, "Desktop");
case SpecialFolder.MyDocuments: // Same value as Personal
return Path.Combine(home, "Documents");
case SpecialFolder.MyMusic:
return Path.Combine(home, "Music");
case SpecialFolder.MyPictures:
return Path.Combine(home, "Pictures");
case SpecialFolder.Templates:
return Path.Combine(home, "Templates");
case SpecialFolder.MyVideos:
return Path.Combine(home, "Videos");
case SpecialFolder.CommonTemplates:
return "/usr/share/templates";
case SpecialFolder.Fonts:
return Path.Combine(home, ".fonts");
case SpecialFolder.UserProfile:
return home;
case SpecialFolder.CommonApplicationData:
return "/usr/share";
default:
return string.Empty;
}
}
}
}