您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

87 行
4.3 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 Alphaleonis.Win32.Filesystem;
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Net.NetworkInformation;
  25. using System.Security;
  26. namespace Alphaleonis.Win32.Network
  27. {
  28. partial class Host
  29. {
  30. /// <summary>Enumerates drives from the local host.</summary>
  31. /// <returns><see cref="IEnumerable{String}"/> drives from the local host.</returns>
  32. /// <exception cref="NetworkInformationException"/>
  33. [SecurityCritical]
  34. public static IEnumerable<string> EnumerateDrives()
  35. {
  36. return EnumerateDrivesCore(null, false);
  37. }
  38. /// <summary>Enumerates local drives from the specified host.</summary>
  39. /// <returns><see cref="IEnumerable{String}"/> drives from the specified host.</returns>
  40. /// <exception cref="NetworkInformationException"/>
  41. /// <param name="host">The DNS or NetBIOS name of the remote server. <see langword="null"/> refers to the local host.</param>
  42. /// <param name="continueOnException">
  43. /// <para><see langword="true"/> suppress any Exception that might be thrown as a result from a failure,</para>
  44. /// <para>such as unavailable resources.</para>
  45. /// </param>
  46. [SecurityCritical]
  47. public static IEnumerable<string> EnumerateDrives(string host, bool continueOnException)
  48. {
  49. return EnumerateDrivesCore(host, continueOnException);
  50. }
  51. /// <summary>Enumerates local drives from the specified host.</summary>
  52. /// <returns><see cref="IEnumerable{String}"/> drives from the specified host.</returns>
  53. /// <exception cref="ArgumentNullException"/>
  54. /// <exception cref="NetworkInformationException"/>
  55. /// <param name="host">The DNS or NetBIOS name of the remote server. <see langword="null"/> refers to the local host.</param>
  56. /// <param name="continueOnException">
  57. /// <para><see langword="true"/> suppress any Exception that might be thrown as a result from a failure,</para>
  58. /// <para>such as unavailable resources.</para>
  59. /// </param>
  60. [SecurityCritical]
  61. private static IEnumerable<string> EnumerateDrivesCore(string host, bool continueOnException)
  62. {
  63. return EnumerateNetworkObjectCore(new FunctionData { EnumType = 1 }, (string structure, SafeGlobalMemoryBufferHandle buffer) =>
  64. structure,
  65. (FunctionData functionData, out SafeGlobalMemoryBufferHandle buffer, int prefMaxLen, out uint entriesRead, out uint totalEntries, out uint resume) =>
  66. {
  67. // When host == null, the local computer is used.
  68. // However, the resulting Host property will be empty.
  69. // So, explicitly state Environment.MachineName to prevent this.
  70. // Furthermore, the UNC prefix: \\ is not required and always removed.
  71. string stripUnc = Utils.IsNullOrWhiteSpace(host) ? Environment.MachineName : Path.GetRegularPathCore(host, GetFullPathOptions.CheckInvalidPathChars, false).Replace(Path.UncPrefix, string.Empty);
  72. return NativeMethods.NetServerDiskEnum(stripUnc, 0, out buffer, NativeMethods.MaxPreferredLength, out entriesRead, out totalEntries, out resume);
  73. }, continueOnException);
  74. }
  75. }
  76. }