WinRTFolderPaths.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. // While all of these give back real paths, most of them are not accessible
  33. // from an appcontainer currently (they will give access denied)
  34. folder switch
  35. {
  36. SpecialFolder.ApplicationData => UserDataPaths.GetDefault().RoamingAppData,
  37. SpecialFolder.CommonApplicationData => AppDataPaths.GetDefault().ProgramData,
  38. SpecialFolder.LocalApplicationData => AppDataPaths.GetDefault().LocalAppData,
  39. SpecialFolder.Cookies => AppDataPaths.GetDefault().Cookies,
  40. SpecialFolder.Desktop => AppDataPaths.GetDefault().Desktop,
  41. SpecialFolder.Favorites => AppDataPaths.GetDefault().Favorites,
  42. SpecialFolder.History => AppDataPaths.GetDefault().History,
  43. SpecialFolder.InternetCache => AppDataPaths.GetDefault().InternetCache,
  44. SpecialFolder.MyMusic => UserDataPaths.GetDefault().Music,
  45. SpecialFolder.MyPictures => UserDataPaths.GetDefault().Pictures,
  46. SpecialFolder.MyVideos => UserDataPaths.GetDefault().Videos,
  47. SpecialFolder.Recent => UserDataPaths.GetDefault().Recent,
  48. SpecialFolder.System => SystemDataPaths.GetDefault().System,
  49. SpecialFolder.Templates => UserDataPaths.GetDefault().Templates,
  50. SpecialFolder.DesktopDirectory => UserDataPaths.GetDefault().Desktop,
  51. SpecialFolder.Personal => UserDataPaths.GetDefault().Documents,
  52. SpecialFolder.CommonDocuments => SystemDataPaths.GetDefault().PublicDocuments,
  53. SpecialFolder.CommonMusic => SystemDataPaths.GetDefault().PublicMusic,
  54. SpecialFolder.CommonPictures => SystemDataPaths.GetDefault().PublicPictures,
  55. SpecialFolder.CommonDesktopDirectory => SystemDataPaths.GetDefault().PublicDesktop,
  56. SpecialFolder.CommonVideos => SystemDataPaths.GetDefault().PublicVideos,
  57. SpecialFolder.UserProfile => UserDataPaths.GetDefault().Profile,
  58. SpecialFolder.SystemX86 => SystemDataPaths.GetDefault().SystemX86,
  59. SpecialFolder.Windows => SystemDataPaths.GetDefault().Windows,
  60. // The following aren't available on WinRT. Our default behavior
  61. // is string.Empty for paths that aren't available:
  62. // SpecialFolder.Programs
  63. // SpecialFolder.MyComputer
  64. // SpecialFolder.SendTo
  65. // SpecialFolder.StartMenu
  66. // SpecialFolder.Startup
  67. // SpecialFolder.ProgramFiles
  68. // SpecialFolder.CommonProgramFiles
  69. // SpecialFolder.AdminTools
  70. // SpecialFolder.CDBurning
  71. // SpecialFolder.CommonAdminTools
  72. // SpecialFolder.CommonOemLinks
  73. // SpecialFolder.CommonStartMenu
  74. // SpecialFolder.CommonPrograms
  75. // SpecialFolder.CommonStartup
  76. // SpecialFolder.CommonTemplates
  77. // SpecialFolder.Fonts
  78. // SpecialFolder.NetworkShortcuts
  79. // SpecialFolder.PrinterShortcuts
  80. // SpecialFolder.CommonProgramFilesX86
  81. // SpecialFolder.ProgramFilesX86
  82. // SpecialFolder.Resources
  83. // SpecialFolder.LocalizedResources
  84. _ => string.Empty,
  85. };
  86. private static string GetFolderPathCoreFallBack(SpecialFolder folder) =>
  87. // For testing without the new WinRT APIs. We cannot use Win32 APIs for
  88. // special folders as they are not in the WACK.
  89. folder switch
  90. {
  91. SpecialFolder.ApplicationData => ApplicationData.Current.RoamingFolder?.Path ?? string.Empty,
  92. SpecialFolder.LocalApplicationData => ApplicationData.Current.LocalFolder?.Path ?? string.Empty,
  93. SpecialFolder.System => SystemDirectory,
  94. SpecialFolder.Windows => Path.GetDirectoryName(SystemDirectory)!,
  95. _ => string.Empty,
  96. };
  97. }
  98. }