WinRTFolderPaths.cs 6.6 KB

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