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.

пре 8 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.Globalization;
  24. namespace Alphaleonis.Win32.Network
  25. {
  26. /// <summary>Contains information about Server Message Block (SMB) shares. This class cannot be inherited.</summary>
  27. [SerializableAttribute]
  28. public sealed class ShareInfo
  29. {
  30. #region Constructor
  31. #region ShareInfo
  32. /// <summary>Creates a <see cref="ShareInfo"/> instance.</summary>
  33. /// <param name="host">A host to retrieve shares from.</param>
  34. /// <param name="shareLevel">One of the <see cref="ShareInfoLevel"/> options.</param>
  35. /// <param name="shareInfo">A <see cref="NativeMethods.SHARE_INFO_2"/> or <see cref="NativeMethods.SHARE_INFO_503"/> instance.</param>
  36. internal ShareInfo(string host, ShareInfoLevel shareLevel, object shareInfo)
  37. {
  38. switch (shareLevel)
  39. {
  40. case ShareInfoLevel.Info1005:
  41. NativeMethods.SHARE_INFO_1005 s1005 = (NativeMethods.SHARE_INFO_1005) shareInfo;
  42. ServerName = host ?? Environment.MachineName;
  43. ResourceType = s1005.shi1005_flags;
  44. break;
  45. case ShareInfoLevel.Info503:
  46. NativeMethods.SHARE_INFO_503 s503 = (NativeMethods.SHARE_INFO_503) shareInfo;
  47. CurrentUses = s503.shi503_current_uses;
  48. MaxUses = s503.shi503_max_uses;
  49. NetName = s503.shi503_netname;
  50. Password = s503.shi503_passwd;
  51. Path = Utils.IsNullOrWhiteSpace(s503.shi503_path) ? null : s503.shi503_path;
  52. Permissions = s503.shi503_permissions;
  53. Remark = s503.shi503_remark;
  54. ServerName = s503.shi503_servername == "*" ? host ?? Environment.MachineName : s503.shi503_servername;
  55. ShareType = s503.shi503_type;
  56. SecurityDescriptor = s503.shi503_security_descriptor;
  57. break;
  58. case ShareInfoLevel.Info2:
  59. NativeMethods.SHARE_INFO_2 s2 = (NativeMethods.SHARE_INFO_2)shareInfo;
  60. CurrentUses = s2.shi2_current_uses;
  61. MaxUses = s2.shi2_max_uses;
  62. NetName = s2.shi2_netname;
  63. Password = s2.shi2_passwd;
  64. Path = Utils.IsNullOrWhiteSpace(s2.shi2_path) ? null : s2.shi2_path;
  65. Permissions = s2.shi2_permissions;
  66. Remark = s2.shi2_remark;
  67. ServerName = host ?? Environment.MachineName;
  68. ShareType = s2.shi2_type;
  69. break;
  70. case ShareInfoLevel.Info1:
  71. NativeMethods.SHARE_INFO_1 s1 = (NativeMethods.SHARE_INFO_1)shareInfo;
  72. CurrentUses = 0;
  73. MaxUses = 0;
  74. NetName = s1.shi1_netname;
  75. Password = null;
  76. Path = null;
  77. Permissions = AccessPermissions.None;
  78. Remark = s1.shi1_remark;
  79. ServerName = host ?? Environment.MachineName;
  80. ShareType = s1.shi1_type;
  81. break;
  82. }
  83. NetFullPath = string.Format(CultureInfo.CurrentCulture, "{0}{1}{2}{3}", Filesystem.Path.UncPrefix, ServerName, Filesystem.Path.DirectorySeparatorChar, NetName);
  84. ShareLevel = shareLevel;
  85. }
  86. #endregion // ShareInfo
  87. #endregion // Constructor
  88. #region Methods
  89. #region ToString
  90. /// <summary>Returns the full path to the share.</summary>
  91. /// <returns>A string that represents this instance.</returns>
  92. public override string ToString()
  93. {
  94. return NetFullPath;
  95. }
  96. #endregion // ToString
  97. #endregion // Methods
  98. #region Properties
  99. #region CurrentUses
  100. /// <summary>The number of current connections to the resource.</summary>
  101. public long CurrentUses { get; private set; }
  102. #endregion // CurrentUses
  103. #region DirectoryInfo
  104. private DirectoryInfo _directoryInfo;
  105. /// <summary>The <see cref="DirectoryInfo"/> instance associated with this share.</summary>
  106. public DirectoryInfo DirectoryInfo
  107. {
  108. get
  109. {
  110. // Do not use ?? expression here.
  111. if (_directoryInfo == null)
  112. _directoryInfo = new DirectoryInfo(null, NetFullPath, PathFormat.FullPath);
  113. return _directoryInfo;
  114. }
  115. }
  116. #endregion // DirectoryInfo
  117. #region NetFullPath
  118. /// <summary>Returns the full UNC path to the share.</summary>
  119. public string NetFullPath { get; internal set; }
  120. #endregion // NetFullPath
  121. #region MaxUses
  122. /// <summary>The maximum number of concurrent connections that the shared resource can accommodate.</summary>
  123. /// <remarks>The number of connections is unlimited if the value specified in this member is –1.</remarks>
  124. public long MaxUses { get; private set; }
  125. #endregion // MaxUses
  126. #region NetName
  127. /// <summary>The name of a shared resource.</summary>
  128. public string NetName { get; private set; }
  129. #endregion // NetName
  130. #region Password
  131. /// <summary>The share's password (when the server is running with share-level security).</summary>
  132. public string Password { get; private set; }
  133. #endregion // Password
  134. #region Path
  135. /// <summary>The local path for the shared resource.</summary>
  136. /// <remarks>For disks, this member is the path being shared. For print queues, this member is the name of the print queue being shared.</remarks>
  137. public string Path { get; private set; }
  138. #endregion // Path
  139. #region Permissions
  140. /// <summary>The shared resource's permissions for servers running with share-level security.</summary>
  141. /// <remarks>Note that Windows does not support share-level security. This member is ignored on a server running user-level security.</remarks>
  142. public AccessPermissions Permissions { get; private set; }
  143. #endregion // Permissions
  144. #region Remark
  145. /// <summary>An optional comment about the shared resource.</summary>
  146. public string Remark { get; private set; }
  147. #endregion // Remark
  148. #region SecurityDescriptor
  149. /// <summary>Specifies the SECURITY_DESCRIPTOR associated with this share.</summary>
  150. public IntPtr SecurityDescriptor { get; private set; }
  151. #endregion // SecurityDescriptor
  152. #region ServerName
  153. /// <summary>A pointer to a string that specifies the DNS or NetBIOS name of the remote server on which the shared resource resides.</summary>
  154. /// <remarks>A value of "*" indicates no configured server name.</remarks>
  155. public string ServerName { get; private set; }
  156. #endregion // ServerName
  157. #region ShareType
  158. /// <summary>The type of share.</summary>
  159. public ShareType ShareType { get; private set; }
  160. #endregion // ShareType
  161. #region ResourceType
  162. private ShareResourceTypes _shareResourceType;
  163. /// <summary>The type of share resource.</summary>
  164. public ShareResourceTypes ResourceType
  165. {
  166. get
  167. {
  168. if (_shareResourceType == ShareResourceTypes.None && !Utils.IsNullOrWhiteSpace(NetName))
  169. _shareResourceType = (Host.GetShareInfoCore(ShareInfoLevel.Info1005, ServerName, NetName, true)).ResourceType;
  170. return _shareResourceType;
  171. }
  172. private set { _shareResourceType = value; }
  173. }
  174. #endregion // ResourceType
  175. #region ShareLevel
  176. /// <summary>The structure level for the ShareInfo instance.</summary>
  177. public ShareInfoLevel ShareLevel { get; private set; }
  178. #endregion // ShareLevel
  179. #endregion // Properties
  180. }
  181. }