You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 rivejä
5.8 KiB

  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. using System.Security;
  25. namespace Alphaleonis.Win32.Filesystem
  26. {
  27. partial class Directory
  28. {
  29. #region .NET
  30. /// <summary>Retrieves the names of the logical drives on this computer in the form "&lt;drive letter&gt;:\".</summary>
  31. /// <returns>An array of type <see cref="String"/> that represents the logical drives on a computer.</returns>
  32. [SecurityCritical]
  33. public static string[] GetLogicalDrives()
  34. {
  35. return EnumerateLogicalDrivesCore(false, false).Select(drive => drive.Name).ToArray();
  36. }
  37. #endregion // .NET
  38. /// <summary>[AlphaFS] Retrieves the names of the logical drives on this computer in the form "&lt;drive letter&gt;:\".</summary>
  39. /// <returns>An array of type <see cref="String"/> that represents the logical drives on a computer.</returns>
  40. /// <param name="fromEnvironment">Retrieve logical drives as known by the Environment.</param>
  41. /// <param name="isReady">Retrieve only when accessible (IsReady) logical drives.</param>
  42. [SecurityCritical]
  43. public static string[] GetLogicalDrives(bool fromEnvironment, bool isReady)
  44. {
  45. return EnumerateLogicalDrivesCore(fromEnvironment, isReady).Select(drive => drive.Name).ToArray();
  46. }
  47. /// <summary>[AlphaFS] Enumerates the drive names of all logical drives on a computer.</summary>
  48. /// <returns>An IEnumerable of type <see cref="Alphaleonis.Win32.Filesystem.DriveInfo"/> that represents the logical drives on a computer.</returns>
  49. /// <param name="fromEnvironment">Retrieve logical drives as known by the Environment.</param>
  50. /// <param name="isReady">Retrieve only when accessible (IsReady) logical drives.</param>
  51. [SecurityCritical]
  52. public static IEnumerable<DriveInfo> EnumerateLogicalDrives(bool fromEnvironment, bool isReady)
  53. {
  54. return EnumerateLogicalDrivesCore(fromEnvironment, isReady);
  55. }
  56. #region Internal Methods
  57. /// <summary>Enumerates the drive names of all logical drives on a computer.</summary>
  58. /// <returns>An IEnumerable of type <see cref="Alphaleonis.Win32.Filesystem.DriveInfo"/> that represents the logical drives on a computer.</returns>
  59. /// <param name="fromEnvironment">Retrieve logical drives as known by the Environment.</param>
  60. /// <param name="isReady">Retrieve only when accessible (IsReady) logical drives.</param>
  61. [SecurityCritical]
  62. internal static IEnumerable<DriveInfo> EnumerateLogicalDrivesCore(bool fromEnvironment, bool isReady)
  63. {
  64. #region Get from Environment
  65. if (fromEnvironment)
  66. {
  67. IEnumerable<string> drivesEnv = isReady
  68. ? Environment.GetLogicalDrives().Where(ld => File.ExistsCore(true, null, ld, PathFormat.FullPath))
  69. : Environment.GetLogicalDrives().Select(ld => ld);
  70. foreach (string drive in drivesEnv)
  71. {
  72. // Optionally check Drive .IsReady.
  73. if (isReady)
  74. {
  75. if (File.ExistsCore(true, null, drive, PathFormat.FullPath))
  76. yield return new DriveInfo(drive);
  77. }
  78. else
  79. yield return new DriveInfo(drive);
  80. }
  81. yield break;
  82. }
  83. #endregion // Get from Environment
  84. #region Get through NativeMethod
  85. uint lastError = NativeMethods.GetLogicalDrives();
  86. if (lastError == Win32Errors.ERROR_SUCCESS)
  87. NativeError.ThrowException((int)lastError);
  88. uint drives = lastError;
  89. int count = 0;
  90. while (drives != 0)
  91. {
  92. if ((drives & 1) != 0)
  93. ++count;
  94. drives >>= 1;
  95. }
  96. string[] result = new string[count];
  97. char[] root = { 'A', Path.VolumeSeparatorChar };
  98. drives = lastError;
  99. count = 0;
  100. while (drives != 0)
  101. {
  102. if ((drives & 1) != 0)
  103. {
  104. string drive = new string(root);
  105. if (isReady)
  106. {
  107. // Optionally check Drive .IsReady.
  108. if (File.ExistsCore(true, null, drive, PathFormat.FullPath))
  109. yield return new DriveInfo(drive);
  110. }
  111. else
  112. {
  113. // Ready or not.
  114. yield return new DriveInfo(drive);
  115. }
  116. result[count++] = drive;
  117. }
  118. drives >>= 1;
  119. root[0]++;
  120. }
  121. #endregion // Get through NativeMethod
  122. }
  123. #endregion // Internal Methods
  124. }
  125. }