Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

I'm trying to map network drive with this code:

 

[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 won't 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,

Some people are wise and some are other-wise.
Posted

found a problem

 

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

...

...

 

//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,

Some people are wise and some are other-wise.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...