views:

112

answers:

0

Hi,

I'm trying to map a drive using WNetAddCOnnection2 but there's something not quite right. The code that I am using from pinvoke.net and seems to work at first. If I am stepping through the code I get a 0 for a response and I am able to use System.IO.Directory.GetFiles() to inspect the new mapped drive which leads me to believe that credentials are fine.

The problem is that the drive is not available outside of the application. When I type net use from a command prompt I see the drive listed like this:

Unavailable L: \\<server>\<share> Microsoft Windows Network

When I try to access the drive I get either:

The system cannot find the drive specified.

or

The system cannot find the path specified.

Any help would be greatly appreciated.

Here's the nutshell of the code in question:

NETRESOURCE res = new NETRESOURCE();
res.iScope = RESOURCE_GLOBALNET;
res.iType = RESOURCETYPE_DISK;
res.iDisplayType = RESOURCEDISPLAYTYPE_SHARE;
res.iUsage = RESOURCEUSAGE_CONNECTABLE;
res.sRemoteName = share;
res.sLocalName = drive;
res.sProvider = null;

int iFlags = 0;
iFlags = CONNECT_UPDATE_PROFILE;

int iResult = WNetAddConnection2( ref res, psPassword, psUsername, iFlags );

The iResult always ends up equaling 0.