Пример #1
0
        internal static string ConnectDisconnectCore(ConnectDisconnectArguments arguments)
        {
            uint lastError;

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


            #region Disconnect

            if (arguments.IsDisconnect)
            {
                bool   force  = arguments.Prompt; // Use value of prompt variable for force value.
                string 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);
            }

            #endregion // Disconnect

            #region Connect

            // arguments.LocalName is allowed to be null or empty.
            //if (Utils.IsNullOrWhiteSpace(arguments.LocalName) && !arguments.IsDeviceMap)
            //   throw new ArgumentNullException("localName");

            if (Utils.IsNullOrWhiteSpace(arguments.RemoteName) && !arguments.IsDeviceMap)
            {
                throw new ArgumentNullException("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);

            #endregion // Connect
        }
Пример #2
0
      internal static string ConnectDisconnectInternal(ConnectDisconnectArguments arguments)
      {
         uint lastError;

         // Always remove backslash for local device.
         if (!Utils.IsNullOrWhiteSpace(arguments.LocalName))
            arguments.LocalName = Path.RemoveTrailingDirectorySeparator(arguments.LocalName, false).ToUpperInvariant();


         #region Disconnect

         if (arguments.IsDisconnect)
         {
            bool force = arguments.Prompt; // Use value of prompt variable for force value.
            string 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;
         }

         #endregion // Disconnect

         #region Connect

         // arguments.LocalName is allowed to be null or empty.
         //if (Utils.IsNullOrWhiteSpace(arguments.LocalName) && !arguments.IsDeviceMap)
         //   throw new ArgumentNullException("localName");

         if (Utils.IsNullOrWhiteSpace(arguments.RemoteName) && !arguments.IsDeviceMap)
            throw new ArgumentNullException("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.Interactive; // The operating system may interact.

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

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

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

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


         // Initialize structure.
         var resource = new NativeMethods.NetResource
         {
            LocalName = arguments.LocalName,
            RemoteName = arguments.RemoteName,
            Type = 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;

         #endregion // Connect
      }