Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

152 рядки
6.2 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.Diagnostics.CodeAnalysis;
  23. using System.Net;
  24. namespace Alphaleonis.Win32.Network
  25. {
  26. /// <summary>Used to create a temporary connection to a network resource that will be disconnected once this instance is disposed.</summary>
  27. public sealed class DriveConnection : IDisposable
  28. {
  29. #region Constructors
  30. /// <summary>Creates a temporary connection to a network resource. The function can redirect a local device to a network resource, using the current user credentials.</summary>
  31. /// <param name="remoteName">The network resource to connect to. The string can be up to MAX_PATH characters in length.</param>
  32. public DriveConnection(string remoteName)
  33. {
  34. Share = remoteName;
  35. LocalName = Host.ConnectDisconnectCore(new Host.ConnectDisconnectArguments
  36. {
  37. RemoteName = Share,
  38. IsDeviceMap = true
  39. });
  40. }
  41. /// <summary>Creates a temporary connection to a network resource. The function can redirect a local device to a network resource, using a user name and password.</summary>
  42. /// <param name="remoteName">The network resource to connect to. The string can be up to MAX_PATH characters in length.</param>
  43. /// <param name="userName">
  44. /// The user name for making the connection. If <paramref name="userName"/> is <see langword="null"/>, the function uses the default
  45. /// user name. (The user context for the process provides the default user name)
  46. /// </param>
  47. /// <param name="password">
  48. /// The password to be used for making the network connection. If <paramref name="password"/> is <see langword="null"/>, the function
  49. /// uses the current default password associated with the user specified by <paramref name="userName"/>.
  50. /// </param>
  51. /// <param name="prompt"><see langword="true"/> always pops-up an authentication dialog box.</param>
  52. public DriveConnection(string remoteName, string userName, string password, bool prompt)
  53. {
  54. Share = remoteName;
  55. LocalName = Host.ConnectDisconnectCore(new Host.ConnectDisconnectArguments
  56. {
  57. RemoteName = Share,
  58. UserName = userName,
  59. Password = password,
  60. Prompt = prompt,
  61. IsDeviceMap = true
  62. });
  63. }
  64. /// <summary>Creates a temporary connection to a network resource. The function can redirect a local device to a network resource, <see cref="NetworkCredential"/> can be supplied.</summary>
  65. /// <param name="remoteName">The network resource to connect to. The string can be up to MAX_PATH characters in length.</param>
  66. /// <param name="credentials">An instance of <see cref="NetworkCredential"/> which provides credentials for password-based authentication schemes such as basic, digest, NTLM, and Kerberos authentication.</param>
  67. /// <param name="prompt"><see langword="true"/> always pops-up an authentication dialog box.</param>
  68. public DriveConnection(string remoteName, NetworkCredential credentials, bool prompt)
  69. {
  70. Share = remoteName;
  71. LocalName = Host.ConnectDisconnectCore(new Host.ConnectDisconnectArguments
  72. {
  73. RemoteName = Share,
  74. Credential = credentials,
  75. Prompt = prompt,
  76. IsDeviceMap = true
  77. });
  78. }
  79. /// <summary><see cref="DriveConnection"/> class destructor.</summary>
  80. ~DriveConnection()
  81. {
  82. Dispose(false);
  83. }
  84. #endregion // Constructors
  85. #region Methods
  86. #region Dispose
  87. /// <summary>Releases all resources used by the <see cref="DriveConnection"/> class.</summary>
  88. public void Dispose()
  89. {
  90. GC.SuppressFinalize(this);
  91. Dispose(true);
  92. }
  93. [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "isDisposing")]
  94. private void Dispose(bool isDisposing)
  95. {
  96. if (!Utils.IsNullOrWhiteSpace(LocalName))
  97. {
  98. Host.ConnectDisconnectCore(new Host.ConnectDisconnectArguments
  99. {
  100. LocalName = LocalName,
  101. Prompt = true, // Use value of prompt variable for force value.
  102. IsDeviceMap = true,
  103. IsDisconnect = true
  104. });
  105. LocalName = null;
  106. }
  107. }
  108. #endregion // Dispose
  109. #region ToString
  110. /// <summary>Returns the last available drive letter used for this connection.</summary>
  111. /// <returns>A string that represents this instance.</returns>
  112. public override string ToString()
  113. {
  114. return LocalName;
  115. }
  116. #endregion // ToString
  117. #endregion // Methods
  118. #region Properties
  119. /// <summary>The last available drive letter used for this connection.</summary>
  120. /// <value>The last available drive letter used for this connection.</value>
  121. public string LocalName { get; private set; }
  122. /// <summary>The path originally specified by the user.</summary>
  123. /// <value>The path originally specified by the user.</value>
  124. public string Share { get; private set; }
  125. #endregion // Properties
  126. }
  127. }