using System; using System.ComponentModel; using System.Runtime.InteropServices; using System.Windows.Forms; using Vanara.Extensions; using static Vanara.PInvoke.Mpr; namespace Vanara.Windows.Forms { /// /// A dialog box that allows the user to browse and connect to network resources. /// /// public class NetworkDisconnectDialog : CommonDialog { private DISCDLGSTRUCT opts; /// Initializes a new instance of the class. public NetworkDisconnectDialog() { opts.cbStructure = (uint)Marshal.SizeOf(typeof(DISCDLGSTRUCT)); } /// Gets or sets a value indicating whether to force when attempting to disconnect from the network resource. /// true if forcing a disconnect; otherwise, false. [DefaultValue(true), Category("Behavior"), Description("Force when attempting to disconnect from the network resource.")] public bool ForceDisconnect { get => !opts.dwFlags.IsFlagSet(DISC.DISC_NO_FORCE); set => opts.dwFlags = opts.dwFlags.SetFlags(DISC.DISC_NO_FORCE, !value); } /// Gets or sets the name of the local device, such as "F:" or "LPT1". /// The name of the local device. [DefaultValue(null), Category("Behavior"), Description("The local device name.")] public string LocalDeviceName { get => opts.lpLocalName; set => opts.lpLocalName = value; } /// Gets or sets the name of the remote network. /// The name of the remote network. [DefaultValue(null), Category("Behavior"), Description("The value displayed in the path field.")] public string RemoteNetworkName { get => opts.lpRemoteName; set => opts.lpRemoteName = value; } /// Gets or sets a value indicating whether to enter the most recently used paths into the combination box. /// true to use MRU path; otherwise, false. /// UseMostRecentPath [DefaultValue(false), Category("Behavior"), Description("Enter the most recently used paths into the combination box.")] public bool UpdateProfile { get => opts.dwFlags.IsFlagSet(DISC.DISC_UPDATE_PROFILE); set { if (value && string.IsNullOrEmpty(opts.lpLocalName)) throw new InvalidOperationException($"{nameof(UpdateProfile)} cannot be set to true if {nameof(LocalDeviceName)} is null or empty."); opts.dwFlags = opts.dwFlags.SetFlags(DISC.DISC_UPDATE_PROFILE, value); } } /// public override void Reset() { opts.dwFlags = 0; opts.lpLocalName = opts.lpRemoteName = null; } /// protected override bool RunDialog(IntPtr hwndOwner) { opts.hwndOwner = hwndOwner; var ret = WNetDisconnectDialog1(opts); if (ret == unchecked((uint)-1)) return false; ret.ThrowIfFailed(); return true; } } }