選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

EnumerateOpenResources.cs 6.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #region EnumerateOpenResources
  31. /// <summary>Enumerates open resources from the local host.</summary>
  32. /// <returns><see cref="IEnumerable{OpenResourceInfo}"/> open resources from the local host.</returns>
  33. /// <exception cref="ArgumentNullException"/>
  34. /// <exception cref="NetworkInformationException"/>
  35. [SecurityCritical]
  36. public static IEnumerable<OpenResourceInfo> EnumerateOpenResources()
  37. {
  38. return EnumerateOpenResourcesCore(null, null, null, false);
  39. }
  40. /// <summary>Enumerates open resources from the specified host.</summary>
  41. /// <returns><see cref="IEnumerable{String}"/> open resources from the specified <paramref name="host"/>.</returns>
  42. /// <exception cref="ArgumentNullException"/>
  43. /// <exception cref="NetworkInformationException"/>
  44. /// <param name="host">The DNS or NetBIOS name of the remote server. <see langword="null"/> refers to the local host.</param>
  45. /// <param name="basePath">
  46. /// This parameter may be <see langword="null"/>. Enumerates only resources that have the value of the basepath parameter as a prefix.
  47. /// (A prefix is the portion of a path that comes before a backslash.)
  48. /// </param>
  49. /// <param name="typeName">
  50. /// This parameter may be <see langword="null"/>. The name of the user or the name of the connection; If <paramref name="typeName"/>
  51. /// does not begin with two backslashes ("\\") it indicates the name of the user. If <paramref name="typeName"/> begins with two
  52. /// backslashes ("\\") it indicates the name of the connection.
  53. /// </param>
  54. /// <param name="continueOnException"><see langword="true"/> suppress any Exception that might be thrown as a result from a failure, such as unavailable resources.</param>
  55. [SecurityCritical]
  56. public static IEnumerable<OpenResourceInfo> EnumerateOpenResources(string host, string basePath, string typeName, bool continueOnException)
  57. {
  58. return EnumerateOpenResourcesCore(host, basePath, typeName, continueOnException);
  59. }
  60. #endregion // EnumerateOpenResources
  61. #region Internal Methods
  62. /// <summary>>Enumerates open resources from the specified host.</summary>
  63. /// <returns><see cref="IEnumerable{String}"/> open resources from the specified <paramref name="host"/>.</returns>
  64. /// <exception cref="ArgumentNullException"/>
  65. /// <exception cref="NetworkInformationException"/>
  66. /// <param name="host">The DNS or NetBIOS name of the remote server. <see langword="null"/> refers to the local host.</param>
  67. /// <param name="basePath">
  68. /// This parameter may be <see langword="null"/>. Enumerates only resources that have the value of the basepath parameter as a prefix.
  69. /// (A prefix is the portion of a path that comes before a backslash.)
  70. /// </param>
  71. /// <param name="typeName">
  72. /// This parameter may be <see langword="null"/>. The name of the user or the name of the connection; If <paramref name="typeName"/>
  73. /// does not begin with two backslashes ("\\") it indicates the name of the user. If <paramref name="typeName"/> begins with two
  74. /// backslashes ("\\") it indicates the name of the connection.
  75. /// </param>
  76. /// <param name="continueOnException"><see langword="true"/> suppress any Exception that might be thrown as a result from a failure, such as unavailable resources.</param>
  77. [SecurityCritical]
  78. private static IEnumerable<OpenResourceInfo> EnumerateOpenResourcesCore(string host, string basePath, string typeName, bool continueOnException)
  79. {
  80. basePath = Utils.IsNullOrWhiteSpace(basePath) ? null : Path.GetRegularPathCore(basePath, GetFullPathOptions.CheckInvalidPathChars, false);
  81. typeName = Utils.IsNullOrWhiteSpace(typeName) ? null : typeName;
  82. var fd = new FunctionData { ExtraData1 = basePath, ExtraData2 = typeName };
  83. return EnumerateNetworkObjectCore(fd, (NativeMethods.FILE_INFO_3 structure, SafeGlobalMemoryBufferHandle buffer) =>
  84. new OpenResourceInfo(host, structure),
  85. (FunctionData functionData, out SafeGlobalMemoryBufferHandle buffer, int prefMaxLen, out uint entriesRead, out uint totalEntries, out uint resumeHandle) =>
  86. {
  87. // When host == null, the local computer is used.
  88. // However, the resulting OpenResourceInfo.Host property will be empty.
  89. // So, explicitly state Environment.MachineName to prevent this.
  90. // Furthermore, the UNC prefix: \\ is not required and always removed.
  91. string stripUnc = Utils.IsNullOrWhiteSpace(host) ? Environment.MachineName : Path.GetRegularPathCore(host, GetFullPathOptions.CheckInvalidPathChars, false).Replace(Path.UncPrefix, string.Empty);
  92. return NativeMethods.NetFileEnum(stripUnc, fd.ExtraData1, fd.ExtraData2, 3, out buffer, NativeMethods.MaxPreferredLength, out entriesRead, out totalEntries, out resumeHandle);
  93. },
  94. continueOnException);
  95. }
  96. #endregion // Internal Methods
  97. }
  98. }