WinRTFolderPaths.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. #if SYSTEM_RUNTIME_WINDOWSRUNTIME_ASSEMBLY
  5. extern alias System_Runtime_Extensions;
  6. using static System_Runtime_Extensions::System.Environment;
  7. #else
  8. using static System.Environment;
  9. #endif
  10. using Windows.Foundation.Metadata;
  11. using Windows.Storage;
  12. using System.IO;
  13. namespace System
  14. {
  15. internal static class WinRTFolderPaths
  16. {
  17. public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
  18. {
  19. // For testing we'll fall back if the needed APIs aren't present.
  20. //
  21. // We're not honoring the special folder options (noverify/create) for a few reasons. One, most of the
  22. // folders always exist (e.g. it is moot). Two, most locations are inaccessible from an appcontainer
  23. // currently - making it impossible to answer the question of existence or create if necessary. Thirdly,
  24. // the Win32 API would create these folders with very specific ACLs, which even in the cases we can create
  25. // are a significant compat risk (trying to replicate internal Windows behavior- it is documented that they
  26. // set specific ACLs, but not which ones).
  27. if (ApiInformation.IsTypePresent("Windows.Storage.UserDataPaths"))
  28. {
  29. return GetFolderPathCoreCurrent(folder);
  30. }
  31. else
  32. {
  33. return GetFolderPathCoreFallBack(folder);
  34. }
  35. }
  36. private static string GetFolderPathCoreCurrent(SpecialFolder folder)
  37. {
  38. // While all of these give back real paths, most of them are not accessible
  39. // from an appcontainer currently (they will give access denied)
  40. switch (folder)
  41. {
  42. case SpecialFolder.ApplicationData:
  43. return UserDataPaths.GetDefault().RoamingAppData;
  44. case SpecialFolder.CommonApplicationData:
  45. return AppDataPaths.GetDefault().ProgramData;
  46. case SpecialFolder.LocalApplicationData:
  47. return AppDataPaths.GetDefault().LocalAppData;
  48. case SpecialFolder.Cookies:
  49. return AppDataPaths.GetDefault().Cookies;
  50. case SpecialFolder.Desktop:
  51. return AppDataPaths.GetDefault().Desktop;
  52. case SpecialFolder.Favorites:
  53. return AppDataPaths.GetDefault().Favorites;
  54. case SpecialFolder.History:
  55. return AppDataPaths.GetDefault().History;
  56. case SpecialFolder.InternetCache:
  57. return AppDataPaths.GetDefault().InternetCache;
  58. case SpecialFolder.MyMusic:
  59. return UserDataPaths.GetDefault().Music;
  60. case SpecialFolder.MyPictures:
  61. return UserDataPaths.GetDefault().Pictures;
  62. case SpecialFolder.MyVideos:
  63. return UserDataPaths.GetDefault().Videos;
  64. case SpecialFolder.Recent:
  65. return UserDataPaths.GetDefault().Recent;
  66. case SpecialFolder.System:
  67. return SystemDataPaths.GetDefault().System;
  68. case SpecialFolder.Templates:
  69. return UserDataPaths.GetDefault().Templates;
  70. case SpecialFolder.DesktopDirectory:
  71. return UserDataPaths.GetDefault().Desktop;
  72. case SpecialFolder.Personal:
  73. return UserDataPaths.GetDefault().Documents;
  74. case SpecialFolder.CommonDocuments:
  75. return SystemDataPaths.GetDefault().PublicDocuments;
  76. case SpecialFolder.CommonMusic:
  77. return SystemDataPaths.GetDefault().PublicMusic;
  78. case SpecialFolder.CommonPictures:
  79. return SystemDataPaths.GetDefault().PublicPictures;
  80. case SpecialFolder.CommonDesktopDirectory:
  81. return SystemDataPaths.GetDefault().PublicDesktop;
  82. case SpecialFolder.CommonVideos:
  83. return SystemDataPaths.GetDefault().PublicVideos;
  84. case SpecialFolder.UserProfile:
  85. return UserDataPaths.GetDefault().Profile;
  86. case SpecialFolder.SystemX86:
  87. return SystemDataPaths.GetDefault().SystemX86;
  88. case SpecialFolder.Windows:
  89. return SystemDataPaths.GetDefault().Windows;
  90. // The following aren't available on WinRT. Our default behavior
  91. // is string.Empty for paths that aren't available.
  92. //
  93. // case SpecialFolder.Programs:
  94. // case SpecialFolder.MyComputer:
  95. // case SpecialFolder.SendTo:
  96. // case SpecialFolder.StartMenu:
  97. // case SpecialFolder.Startup:
  98. // case SpecialFolder.ProgramFiles:
  99. // case SpecialFolder.CommonProgramFiles:
  100. // case SpecialFolder.AdminTools:
  101. // case SpecialFolder.CDBurning:
  102. // case SpecialFolder.CommonAdminTools:
  103. // case SpecialFolder.CommonOemLinks:
  104. // case SpecialFolder.CommonStartMenu:
  105. // case SpecialFolder.CommonPrograms:
  106. // case SpecialFolder.CommonStartup:
  107. // case SpecialFolder.CommonTemplates:
  108. // case SpecialFolder.Fonts:
  109. // case SpecialFolder.NetworkShortcuts:
  110. // case SpecialFolder.PrinterShortcuts:
  111. // case SpecialFolder.CommonProgramFilesX86:
  112. // case SpecialFolder.ProgramFilesX86:
  113. // case SpecialFolder.Resources:
  114. // case SpecialFolder.LocalizedResources:
  115. default:
  116. return string.Empty;
  117. }
  118. }
  119. private static string GetFolderPathCoreFallBack(SpecialFolder folder)
  120. {
  121. // For testing without the new WinRT APIs. We cannot use Win32 APIs for
  122. // special folders as they are not in the WACK.
  123. switch (folder)
  124. {
  125. case SpecialFolder.ApplicationData:
  126. return ApplicationData.Current.RoamingFolder?.Path;
  127. case SpecialFolder.LocalApplicationData:
  128. return ApplicationData.Current.LocalFolder?.Path;
  129. case SpecialFolder.System:
  130. return SystemDirectory;
  131. case SpecialFolder.Windows:
  132. return Path.GetDirectoryName(SystemDirectory);
  133. default:
  134. return string.Empty;
  135. }
  136. }
  137. }
  138. }