0

I know I can use the WNetAddConnection2 and similar variants to connect to remote shares using custom credentials. I would like to do the same for mapped drives (ex:\\MyShare\MyFolder mapped to Y:) and my local disks (eg: C:,D:,E:, etc...)
If I point WNetAddConnection to something not \\ it fails...

Is there a function specific for this scenario???

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Leonardo
  • 10,737
  • 10
  • 62
  • 155

1 Answers1

0

At work we use a WindowsImpersonationContext to handle this situation. Originally we used it in a VB.NET ASP.NET application, but it is readily modified.

Microsoft Support has a great article on how to do it. (We originally used it for Network Drives, but it can be expanded to local disks as well.)

You can basically impersonate a valid user account, which will grant you permissions that account would have in the situation. I had to use it to get access to a network drive at work, on a computer that was not a part of the domain. (As such, we had to provide a Username, Domain, and Password for this user account that was not on our domain network and this topic served us well.)

Der Kommissar
  • 5,848
  • 1
  • 29
  • 43
  • @Leonardo Can you be specific on how it fails? I'm curious if your setup has anything to do with it. – Der Kommissar Jun 01 '15 at 20:20
  • actually its a more generic, not involving mapped drives... it only fails on local drives... when i try to copy from folder A to B impersonation User X where A and B do not share the same permissions (eg: X can read from A but not write on B)... so File.Copy crashes... – Leonardo Jun 02 '15 at 18:58