Environment.WinRT.cs 6.7 KB

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