/* 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
{
/// Used to create a temporary connection to a network resource that will be disconnected once this instance is disposed.
public sealed class DriveConnection : IDisposable
{
#region Constructors
/// Creates a temporary connection to a network resource. The function can redirect a local device to a network resource, using the current user credentials.
/// The network resource to connect to. The string can be up to MAX_PATH characters in length.
public DriveConnection(string remoteName)
{
Share = remoteName;
LocalName = Host.ConnectDisconnectCore(new Host.ConnectDisconnectArguments
{
RemoteName = Share,
IsDeviceMap = true
});
}
/// 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.
/// The network resource to connect to. The string can be up to MAX_PATH characters in length.
///
/// The user name for making the connection. If is , the function uses the default
/// user name. (The user context for the process provides the default user name)
///
///
/// The password to be used for making the network connection. If is , the function
/// uses the current default password associated with the user specified by .
///
/// always pops-up an authentication dialog box.
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
});
}
/// Creates a temporary connection to a network resource. The function can redirect a local device to a network resource, can be supplied.
/// The network resource to connect to. The string can be up to MAX_PATH characters in length.
/// An instance of which provides credentials for password-based authentication schemes such as basic, digest, NTLM, and Kerberos authentication.
/// always pops-up an authentication dialog box.
public DriveConnection(string remoteName, NetworkCredential credentials, bool prompt)
{
Share = remoteName;
LocalName = Host.ConnectDisconnectCore(new Host.ConnectDisconnectArguments
{
RemoteName = Share,
Credential = credentials,
Prompt = prompt,
IsDeviceMap = true
});
}
/// class destructor.
~DriveConnection()
{
Dispose(false);
}
#endregion // Constructors
#region Methods
#region Dispose
/// Releases all resources used by the class.
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
/// Returns the last available drive letter used for this connection.
/// A string that represents this instance.
public override string ToString()
{
return LocalName;
}
#endregion // ToString
#endregion // Methods
#region Properties
/// The last available drive letter used for this connection.
/// The last available drive letter used for this connection.
public string LocalName { get; private set; }
/// The path originally specified by the user.
/// The path originally specified by the user.
public string Share { get; private set; }
#endregion // Properties
}
}