|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
- using System;
- using System.Diagnostics.CodeAnalysis;
- using System.Net;
-
- namespace Alphaleonis.Win32.Network
- {
- /// <summary>Used to create a temporary connection to a network resource that will be disconnected once this instance is disposed.</summary>
- public sealed class DriveConnection : IDisposable
- {
- #region Constructors
-
- /// <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>
- /// <param name="remoteName">The network resource to connect to. The string can be up to MAX_PATH characters in length.</param>
- public DriveConnection(string remoteName)
- {
- Share = remoteName;
-
- LocalName = Host.ConnectDisconnectCore(new Host.ConnectDisconnectArguments
- {
- RemoteName = Share,
- IsDeviceMap = true
- });
- }
-
- /// <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>
- /// <param name="remoteName">The network resource to connect to. The string can be up to MAX_PATH characters in length.</param>
- /// <param name="userName">
- /// The user name for making the connection. If <paramref name="userName"/> is <see langword="null"/>, the function uses the default
- /// user name. (The user context for the process provides the default user name)
- /// </param>
- /// <param name="password">
- /// The password to be used for making the network connection. If <paramref name="password"/> is <see langword="null"/>, the function
- /// uses the current default password associated with the user specified by <paramref name="userName"/>.
- /// </param>
- /// <param name="prompt"><see langword="true"/> always pops-up an authentication dialog box.</param>
- public DriveConnection(string remoteName, string userName, string password, bool prompt)
- {
- Share = remoteName;
-
- LocalName = Host.ConnectDisconnectCore(new Host.ConnectDisconnectArguments
- {
- RemoteName = Share,
- UserName = userName,
- Password = password,
- Prompt = prompt,
- IsDeviceMap = true
- });
- }
-
- /// <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>
- /// <param name="remoteName">The network resource to connect to. The string can be up to MAX_PATH characters in length.</param>
- /// <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>
- /// <param name="prompt"><see langword="true"/> always pops-up an authentication dialog box.</param>
- public DriveConnection(string remoteName, NetworkCredential credentials, bool prompt)
- {
- Share = remoteName;
-
- LocalName = Host.ConnectDisconnectCore(new Host.ConnectDisconnectArguments
- {
- RemoteName = Share,
- Credential = credentials,
- Prompt = prompt,
- IsDeviceMap = true
- });
- }
-
- /// <summary><see cref="DriveConnection"/> class destructor.</summary>
- ~DriveConnection()
- {
- Dispose(false);
- }
-
- #endregion // Constructors
-
- #region Methods
-
- #region Dispose
-
- /// <summary>Releases all resources used by the <see cref="DriveConnection"/> class.</summary>
- public void Dispose()
- {
- GC.SuppressFinalize(this);
- Dispose(true);
- }
-
- [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "isDisposing")]
- private void Dispose(bool isDisposing)
- {
- if (!Utils.IsNullOrWhiteSpace(LocalName))
- {
- Host.ConnectDisconnectCore(new Host.ConnectDisconnectArguments
- {
- LocalName = LocalName,
- Prompt = true, // Use value of prompt variable for force value.
- IsDeviceMap = true,
- IsDisconnect = true
- });
-
- LocalName = null;
- }
- }
-
- #endregion // Dispose
-
- #region ToString
-
- /// <summary>Returns the last available drive letter used for this connection.</summary>
- /// <returns>A string that represents this instance.</returns>
- public override string ToString()
- {
- return LocalName;
- }
-
- #endregion // ToString
-
- #endregion // Methods
-
- #region Properties
-
- /// <summary>The last available drive letter used for this connection.</summary>
- /// <value>The last available drive letter used for this connection.</value>
- public string LocalName { get; private set; }
-
- /// <summary>The path originally specified by the user.</summary>
- /// <value>The path originally specified by the user.</value>
- public string Share { get; private set; }
-
- #endregion // Properties
- }
- }
|