Esempio n. 1
0
        /// <summary>
        /// Map a network drive.
        /// </summary>
        /// <param name="unc">The full UNC path.</param>
        /// <param name="drive">The drive letter (e.g. "Z:", "X:", etc.)</param>
        /// <param name="user">The username, null if you want the current user.</param>
        /// <param name="password">The password, null to use the default password.</param>
        /// <returns>The error code of the WNetAddConnection2 function.</returns>
        public static void MapNetworkDrive(string unc, string drive, string user, string password)
        {
            SafeNativeMethods.NETRESOURCE myNetResource = new SafeNativeMethods.NETRESOURCE();
            myNetResource.lpLocalName  = drive;
            myNetResource.lpRemoteName = unc;

            // Attempt the drive map
            int result = SafeNativeMethods.WNetAddConnection2(myNetResource, password, user, 0);

            // Check for errors
            if (result != 0)
            {
                string message = string.Format("Failed to map drive {0}", unc);
                if (result == 86 ||   // ERROR_INVALID_PASSWORD
                    result == 2202 || // ERROR_BAD_USERNAME
                    result == 1326)   // ERROR_LOGON_FAILURE
                {
                    message += ": invalid username or password.";
                }
                else
                {
                    message += string.Format(": native error code = {0}", result);
                }

                throw new Win32Exception(result, message);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Map a network drive.
        /// </summary>
        /// <param name="unc">The full UNC path.</param>
        /// <param name="drive">The drive letter (e.g. "Z:", "X:", etc.)</param>
        /// <param name="user">The username, null if you want the current user.</param>
        /// <param name="password">The password, null to use the default password.</param>
        /// <returns>The error code of the WNetAddConnection2 function.</returns>
        public static int MapNetworkDrive(string unc, string drive, string user, string password)
        {
            SafeNativeMethods.NETRESOURCE myNetResource = new SafeNativeMethods.NETRESOURCE();
            myNetResource.lpLocalName  = drive;
            myNetResource.lpRemoteName = unc;
            int result = SafeNativeMethods.WNetAddConnection2(myNetResource, password, user, 0);

            return(result);
        }
Esempio n. 3
0
 /// <summary>
 /// Map a network drive.
 /// </summary>
 /// <param name="unc">The full UNC path.</param>
 /// <param name="drive">The drive letter (e.g. "Z:", "X:", etc.)</param>
 /// <param name="user">The username, null if you want the current user.</param>
 /// <param name="password">The password, null to use the default password.</param>
 /// <returns>The error code of the WNetAddConnection2 function.</returns>
 public static int MapNetworkDrive(string unc, string drive, string user, string password)
 {
     SafeNativeMethods.NETRESOURCE myNetResource = new SafeNativeMethods.NETRESOURCE();
     myNetResource.lpLocalName = drive;
     myNetResource.lpRemoteName = unc;
     int result = SafeNativeMethods.WNetAddConnection2(myNetResource, password, user, 0);
     return result;
 }
Esempio n. 4
0
        /// <summary>
        /// connect to a remote computer
        /// based on http://social.msdn.microsoft.com/Forums/et-EE/netfxbcl/thread/58159e0e-aa45-4d46-a128-596c3d23ff5c
        /// </summary>
        /// <param name="remoteUNC"></param>
        /// <param name="username">better use the domainname\username format</param>
        /// <param name="password"></param>
        /// <param name="promptUser">true to for a promt, if needed</param>
        /// <returns></returns>
        public static Boolean MapNetworkDrive(string remoteUNC, string username, string password, bool promptUser)
        {
            SafeNativeMethods.NETRESOURCE nr = new SafeNativeMethods.NETRESOURCE();
            nr.dwType = SafeNativeMethods.ResourceType.RESOURCETYPE_DISK;
            nr.lpRemoteName = remoteUNC;
            // nr.lpLocalName = "F:";

            int ret;
            if (promptUser)
            {
                ret = SafeNativeMethods.WNetUseConnection(IntPtr.Zero, nr, "", "", SafeNativeMethods.Connect.CONNECT_INTERACTIVE | SafeNativeMethods.Connect.CONNECT_PROMPT, null, null, null);
            }
            else
            {
                ret = SafeNativeMethods.WNetUseConnection(IntPtr.Zero, nr, password, username, 0, null, null, null);
            }

            if (ret != 0)
            {
                LibraryLogging.Error("Unable to connect to {0} as {1} Error:{2} {3}", remoteUNC, username, ret, LastError());
                return false;
            }

            return true;
        }
Esempio n. 5
0
        /// <summary>
        /// Map a network drive.
        /// </summary>
        /// <param name="unc">The full UNC path.</param>
        /// <param name="drive">The drive letter (e.g. "Z:", "X:", etc.)</param>
        /// <param name="user">The username, null if you want the current user.</param>
        /// <param name="password">The password, null to use the default password.</param>
        /// <returns>The error code of the WNetAddConnection2 function.</returns>
        public static void MapNetworkDrive(string unc, string drive, string user, string password)
        {
            SafeNativeMethods.NETRESOURCE myNetResource = new SafeNativeMethods.NETRESOURCE();
            myNetResource.lpLocalName = drive;
            myNetResource.lpRemoteName = unc;

            // Attempt the drive map
            int result = SafeNativeMethods.WNetAddConnection2(myNetResource, password, user, 0);

            // Check for errors
            if (result != 0)
            {
                string message = string.Format("Failed to map drive {0}", unc);
                if( result == 86 ||  // ERROR_INVALID_PASSWORD
                    result == 2202 ||  // ERROR_BAD_USERNAME
                    result == 1326 )  // ERROR_LOGON_FAILURE
                {
                    message += ": invalid username or password.";
                }
                else
                {
                    message += string.Format(": native error code = {0}", result);
                }

                throw new Win32Exception(result, message);
            }
        }