コード例 #1
0
 private void Connect()
 {
     GC.ReRegisterForFinalize(this);
     this.IsOpen = false;
     this.Error  = WindowsNetwork.OpenConnection(this.Resource, this.Username, this.Password, this.Interactive);
     this.IsOpen = true;
 }
コード例 #2
0
        /// <summary>
        ///     Closes an existing connection to a Windows network resource.
        /// </summary>
        /// <param name="resource"> The path of the resource (e.g. a UNC path to a file share). </param>
        /// <param name="force"> true if the connection is to be forcibly closed, even if the connection is still used. </param>
        /// <remarks>
        ///     <para>
        ///         No indication (return value or exception) is provided to determine whether closing the connection suceeded.
        ///     </para>
        ///     <note type="important">
        ///         Whether and how the connection is closed depends heavily on the configuration of Windows, the computer, the
        ///         network, and the remote resource itself.
        ///         It cannot be guaranteed that the connection is really being closed.
        ///         Therefore, this method should only be seen as a &quot;hint to Windows what to do&quot;.
        ///     </note>
        /// </remarks>
        /// <exception cref="ArgumentNullException"> <paramref name="resource" /> is null. </exception>
        /// <exception cref="ArgumentException"> <paramref name="resource" /> is an empty string. </exception>
        public static void CloseConnection(string resource, bool force)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            if (string.IsNullOrWhiteSpace(resource))
            {
                throw new ArgumentException("The string is empty", nameof(resource));
            }

            WindowsNetwork.WNetCancelConnection2(resource, 0, force);
        }
コード例 #3
0
 private void Dispose(bool disposing)
 {
     this.IsOpen = false;
     WindowsNetwork.CloseConnection(this.Resource, true);
 }
コード例 #4
0
        /// <summary>
        ///     Establishes a connection to a Windows network resource.
        /// </summary>
        /// <param name="resource"> The path of the resource (e.g. a UNC path to a file share). </param>
        /// <param name="username"> The logon name under which the connection is to be established (null for the current user). </param>
        /// <param name="password"> The password for the logon if <paramref name="username" /> is specified. </param>
        /// <param name="interactive">
        ///     Specifies whether an interactive logon can be performed if necessary (e.g. the user might be
        ///     asked to enter credentials).
        /// </param>
        /// <returns>
        ///     The error which occurred during establishing the network connection.
        ///     <see cref="WindowsNetworkError.None" /> is returned if the connection was established successfully.
        /// </returns>
        /// <remarks>
        ///     <note type="important">
        ///         Whether and how the connection is established depends heavily on the configuration of Windows, the computer,
        ///         the network, and the remote resource itself.
        ///         This is even more true for using interactive logon (<paramref name="interactive" />).
        ///     </note>
        /// </remarks>
        /// <exception cref="ArgumentNullException"> <paramref name="resource" /> is null. </exception>
        /// <exception cref="ArgumentException"> <paramref name="resource" /> is an empty string. </exception>
        /// <exception cref="Win32Exception">
        ///     An unknown error occurred which could not be translated to
        ///     <see cref="WindowsNetworkError" />.
        /// </exception>
        public static WindowsNetworkError OpenConnection(string resource, string username, string password,
                                                         bool interactive)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            if (string.IsNullOrWhiteSpace(resource))
            {
                throw new ArgumentException("The string is empty", nameof(resource));
            }

            NETRESOURCE connection = new NETRESOURCE();

            connection.dwType     = WindowsNetwork.ResourcetypeAny;
            connection.LocalName  = null;
            connection.RemoteName = resource;
            connection.Provider   = null;

            int errorCode = WindowsNetwork.WNetAddConnection3(IntPtr.Zero, ref connection, password, username,
                                                              interactive ? (WindowsNetwork.ConnectInteractive |
                                                                             WindowsNetwork.ConnectPrompt) : 0);

            WindowsNetworkError error;

            switch (errorCode)
            {
            default:
            {
                string errorMessage = WindowsApi.GetErrorMessage(errorCode);
                throw new Win32Exception(errorCode, errorMessage);
            }

            case (int)WindowsError.ErrorSuccess:
            {
                error = WindowsNetworkError.None;
                break;
            }

            case (int)WindowsError.ErrorBadNetName:
            {
                error = WindowsNetworkError.InvalidParameters;
                break;
            }

            case (int)WindowsError.ErrorInvalidPassword:
            {
                error = WindowsNetworkError.InvalidPassword;
                break;
            }

            case (int)WindowsError.ErrorCancelled:
            {
                error = WindowsNetworkError.CanceledByUser;
                break;
            }

            case (int)WindowsError.ErrorBusy:
            {
                error = WindowsNetworkError.Busy;
                break;
            }

            case (int)WindowsError.ErrorNoNetOrBadPath:
            {
                error = WindowsNetworkError.Unavailable;
                break;
            }

            case (int)WindowsError.ErrorNoNetwork:
            {
                error = WindowsNetworkError.Unavailable;
                break;
            }
            }

            return(error);
        }