Map drive

sizer

Well-known member
Joined
Mar 6, 2003
Messages
123
Location
Croatia
Hi,
Im trying to map network drive with this code:

C#:
[System.Runtime.InteropServices.DllImport("mpr.dll", EntryPoint="WNetAddConnection2A",SetLastError=true)]
		public static extern int WNetAddConnection2(ref NETRESOURCE lpNetResource, string lpPassword, string lpUserName, int dwFlags);
		
		[System.Runtime.InteropServices.DllImport("mpr", EntryPoint="WNetCancelConnection2A")]
		public static extern int WNetCancelConnection2(string lpName, int dwFlags, int fForce);
		
		
		
		[StructLayout(LayoutKind.Sequential)]

			public struct NETRESOURCE
		{
			public int dwScope;
			public int dwType;
			public int dwDisplayType;
			public int dwUsage;
			public string lpLocalName;
			public string lpRemoteName;
			public string lpComment;
			public string lpProvider;
		}
		public const int ForceDisconnect = 1;
		public const long RESOURCETYPE_DISK = 1;
		public const Int32 CONNECT_UPDATE_PROFILE = 0x1;

		public bool MapDrive(string DriveLetter, string UNCPath,string username,string password)
		{
			NETRESOURCE nr;
			string strUsername;
			string strPassword;
			nr = new NETRESOURCE();
			nr.lpRemoteName = @"\\127.0.0.1\share";//UNCPath.ToString();
			nr.lpLocalName ="Z:"; //DriveLetter.ToString() + ":";
			strUsername ="SomeUser"; //username;
			strPassword ="SomePass"; //password;
			nr.dwType = (int)RESOURCETYPE_DISK;
			int result;
			result = WNetAddConnection2( ref nr, strUsername, strPassword, 0);
			if (result == 0) 
			{
				return true;
			} 
			else 
			{
				
				throw new Win32Exception(result);
				//return false;
			}
		}

Everthing works fine (for Workgroup), but on domain wont work, even if i put for username=DOMAIN\user (user is domain admin, full permissions are granted).
Following exception is thrown:
Logon failure: unknown user name or bad password
Any suggestions?
Regards,
 
found a problem

I found the problem :eek: :eek: :eek:
...
...

C#:
//WRONG
WNetAddConnection2( ref nr, strUsername, strPassword, 0);

//CORRECT
WNetAddConnection2( ref nr, strPassword, strUsername, 0);
LoL
When you test with same username and password (like me) than the first one is correct too :))))))
...
...

Now works perfect :).
Regards,
 
Back
Top