1 // Copyright 2014 Xamarin Inc.
3 // note: or older hack to give the Documents (or Library) directories
6 using System
.Runtime
.InteropServices
;
10 public static partial class Environment
{
12 static string ns_document
;
13 static string ns_library
;
15 [DllImport ("__Internal")]
16 extern static string xamarin_GetFolderPath (int folder
);
18 static string NSDocumentDirectory
{
20 if (ns_document
== null) {
22 // The "normal" NSDocumentDirectory is a read-only directory on tvOS
23 // and that breaks a lot of assumptions in the runtime and the BCL
24 // to avoid this we relocate the Documents directory under Caches
25 ns_document
= Path
.Combine (NSLibraryDirectory
, "Caches", "Documents");
26 if (!Directory
.Exists (ns_document
))
27 Directory
.CreateDirectory (ns_document
);
29 ns_document
= xamarin_GetFolderPath (/* NSDocumentDirectory */ 9);
36 // Various user-visible documentation, support, and configuration files
37 static string NSLibraryDirectory
{
39 if (ns_library
== null)
40 ns_library
= xamarin_GetFolderPath (/* NSLibraryDirectory */ 5);
45 public static string GetFolderPath (SpecialFolder folder
, SpecialFolderOption option
)
47 return UnixGetFolderPath (folder
, option
);
50 // needed by our BCL, e.g. IsolatedStorageFile.cs
51 internal static string UnixGetFolderPath (SpecialFolder folder
, SpecialFolderOption option
)
53 var dir
= iOSGetFolderPath (folder
);
54 if ((option
== SpecialFolderOption
.Create
) && !Directory
.Exists (dir
))
55 Directory
.CreateDirectory (dir
);
59 internal static string iOSGetFolderPath (SpecialFolder folder
)
62 case SpecialFolder
.MyComputer
:
63 case SpecialFolder
.Programs
:
64 case SpecialFolder
.SendTo
:
65 case SpecialFolder
.StartMenu
:
66 case SpecialFolder
.Startup
:
67 case SpecialFolder
.Cookies
:
68 case SpecialFolder
.History
:
69 case SpecialFolder
.Recent
:
70 case SpecialFolder
.CommonProgramFiles
:
71 case SpecialFolder
.System
:
72 case SpecialFolder
.NetworkShortcuts
:
73 case SpecialFolder
.CommonStartMenu
:
74 case SpecialFolder
.CommonPrograms
:
75 case SpecialFolder
.CommonStartup
:
76 case SpecialFolder
.CommonDesktopDirectory
:
77 case SpecialFolder
.PrinterShortcuts
:
78 case SpecialFolder
.Windows
:
79 case SpecialFolder
.SystemX86
:
80 case SpecialFolder
.ProgramFilesX86
:
81 case SpecialFolder
.CommonProgramFilesX86
:
82 case SpecialFolder
.CommonDocuments
:
83 case SpecialFolder
.CommonAdminTools
:
84 case SpecialFolder
.AdminTools
:
85 case SpecialFolder
.CommonMusic
:
86 case SpecialFolder
.CommonPictures
:
87 case SpecialFolder
.CommonVideos
:
88 case SpecialFolder
.LocalizedResources
:
89 case SpecialFolder
.CommonOemLinks
:
90 case SpecialFolder
.CDBurning
:
94 case SpecialFolder
.Personal
:
95 case SpecialFolder
.LocalApplicationData
:
96 return NSDocumentDirectory
;
98 case SpecialFolder
.ApplicationData
:
99 // note: at first glance that looked like a good place to return NSLibraryDirectory
100 // but it would break isolated storage for existing applications
101 return Path
.Combine (NSDocumentDirectory
, ".config");
103 case SpecialFolder
.Resources
:
104 return NSLibraryDirectory
; // older (8.2 and previous) would return String.Empty
106 case SpecialFolder
.Desktop
:
107 case SpecialFolder
.DesktopDirectory
:
108 return Path
.Combine (NSDocumentDirectory
, "Desktop");
110 case SpecialFolder
.MyMusic
:
111 return Path
.Combine (NSDocumentDirectory
, "Music");
113 case SpecialFolder
.MyPictures
:
114 return Path
.Combine (NSDocumentDirectory
, "Pictures");
116 case SpecialFolder
.Templates
:
117 return Path
.Combine (NSDocumentDirectory
, "Templates");
119 case SpecialFolder
.MyVideos
:
120 return Path
.Combine (NSDocumentDirectory
, "Videos");
122 case SpecialFolder
.CommonTemplates
:
123 return "/usr/share/templates";
125 case SpecialFolder
.Fonts
:
126 return Path
.Combine (NSDocumentDirectory
, ".fonts");
128 case SpecialFolder
.Favorites
:
129 return Path
.Combine (NSLibraryDirectory
, "Favorites");
131 case SpecialFolder
.ProgramFiles
:
132 return "/Applications";
134 case SpecialFolder
.InternetCache
:
135 return Path
.Combine (NSLibraryDirectory
, "Caches");
137 case SpecialFolder
.UserProfile
:
138 return internalGetHome ();
140 case SpecialFolder
.CommonApplicationData
:
144 throw new ArgumentException ("Invalid SpecialFolder");