Beispiel #1
0
 /// <summary>Initializes a new instance of the <see cref="DfsStorageInfo"/> class, which acts as a wrapper for a DFS root or link target.</summary>
 /// <param name="structure">An initialized <see cref="NativeMethods.DfsStorageInfo"/> instance.</param>
 internal DfsStorageInfo(NativeMethods.DfsStorageInfo structure)
 {
    ServerName = structure.ServerName;
    ShareName = structure.ShareName;
    State = structure.State;
 }
Beispiel #2
0
        internal static string ConnectDisconnectCore(ConnectDisconnectArguments arguments)
        {
            uint lastError;

            // Always remove backslash.
            if (!Utils.IsNullOrWhiteSpace(arguments.LocalName))
            {
                arguments.LocalName = Path.RemoveTrailingDirectorySeparator(arguments.LocalName).ToUpperInvariant();
            }


            if (!Utils.IsNullOrWhiteSpace(arguments.RemoteName))
            {
                if (!arguments.RemoteName.StartsWith(Path.UncPrefix, StringComparison.Ordinal))
                {
                    arguments.RemoteName = Path.UncPrefix + arguments.RemoteName;
                }


                // Always remove backslash.
                if (!Utils.IsNullOrWhiteSpace(arguments.RemoteName))
                {
                    arguments.RemoteName = Path.RemoveTrailingDirectorySeparator(arguments.RemoteName);
                }
            }


            // Disconnect

            if (arguments.IsDisconnect)
            {
                var force  = arguments.Prompt; // Use value of prompt variable for force value.
                var target = arguments.IsDeviceMap ? arguments.LocalName : arguments.RemoteName;

                if (Utils.IsNullOrWhiteSpace(target))
                {
                    throw new ArgumentNullException(arguments.IsDeviceMap ? "localName" : "remoteName");
                }


                lastError = NativeMethods.WNetCancelConnection(target, arguments.UpdateProfile ? NativeMethods.Connect.UpdateProfile : NativeMethods.Connect.None, force);

                if (lastError != Win32Errors.NO_ERROR)
                {
                    throw new NetworkInformationException((int)lastError);
                }

                return(null);
            }


            // Connect

            // arguments.LocalName is allowed to be null or empty.

            if (Utils.IsNullOrWhiteSpace(arguments.RemoteName) && !arguments.IsDeviceMap)
            {
                throw new ArgumentNullException("arguments.RemoteName");
            }


            // When supplied, use data from NetworkCredential instance.
            if (arguments.Credential != null)
            {
                arguments.UserName = Utils.IsNullOrWhiteSpace(arguments.Credential.Domain)
               ? arguments.Credential.UserName
               : string.Format(CultureInfo.InvariantCulture, @"{0}\{1}", arguments.Credential.Domain, arguments.Credential.UserName);

                arguments.Password = arguments.Credential.Password;
            }


            // Assemble Connect arguments.
            var connect = NativeMethods.Connect.None;

            if (arguments.IsDeviceMap)
            {
                connect = connect | NativeMethods.Connect.Redirect;
            }

            if (arguments.Prompt)
            {
                connect = connect | NativeMethods.Connect.Prompt | NativeMethods.Connect.Interactive;
            }

            if (arguments.UpdateProfile)
            {
                connect = connect | NativeMethods.Connect.UpdateProfile;
            }

            if (arguments.SaveCredentials)
            {
                connect = connect | NativeMethods.Connect.SaveCredentialManager;
            }


            // Initialize structure.
            var resource = new NativeMethods.NETRESOURCE
            {
                lpLocalName  = arguments.LocalName,
                lpRemoteName = arguments.RemoteName,
                dwType       = NativeMethods.ResourceType.Disk
            };

            // Three characters for: "X:\0" (Drive X: with null terminator)
            uint          bufferSize = 3;
            StringBuilder buffer;

            do
            {
                buffer = new StringBuilder((int)bufferSize);

                uint result;
                lastError = NativeMethods.WNetUseConnection(arguments.WinOwner, ref resource, arguments.Password, arguments.UserName, connect, buffer, out bufferSize, out result);

                switch (lastError)
                {
                case Win32Errors.NO_ERROR:
                    break;

                case Win32Errors.ERROR_MORE_DATA:
                    // MSDN, lpBufferSize: If the call fails because the buffer is not large enough,
                    // the function returns the required buffer size in this location.
                    //
                    // Windows 8 x64: bufferSize remains unchanged.

                    bufferSize = bufferSize * 2;
                    break;
                }
            } while (lastError == Win32Errors.ERROR_MORE_DATA);


            if (lastError != Win32Errors.NO_ERROR)
            {
                throw new NetworkInformationException((int)lastError);
            }


            return(arguments.IsDeviceMap ? buffer.ToString() : null);
        }