WNetAddConnection2 is on c++
public class Drive Settings
{
private enum ResourceScope
{
RESOURCE_CONNECTED = 1 ,
RESOURCE_GLOBALNET,
RESOURCE_REMEMBERED,
RESOURCE_RECENT,
RESOURCE_CONTEXT
}
private enum ResourceType
{
RESOURCETYPE_ANY,
RESOURCETYPE_DISK,
RESOURCETYPE_PRINT,
RESOURCETYPE_RESERVED
}
private enum ResourceUsage
{
RESOURCEUSAGE_CONNECTABLE = 0x00000001,
RESOURCEUSAGE_CONTAINER = 0x00000002,
RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
RESOURCEUSAGE_SIBLING = 0x00000008,
RESOURCEUSAGE_ATTACHED = 0x00000010
}
private enum ResourceDisplayType
{
RESOURCEDISPLAYTYPE_GENERIC,
RESOURCEDISPLAYTYPE_DOMAIN,
RESOURCEDISPLAYTYPE_SERVER,
RESOURCEDISPLAYTYPE_SHARE,
RESOURCEDISPLAYTYPE_FILE,
RESOURCEDISPLAYTYPE_GROUP,
RESOURCEDISPLAYTYPE_NETWORK ,
RESOURCEDISPLAYTYPE_ROOT,
RESOURCEDISPLAYTYPE_SHAREADMIN,
RESOURCEDISPLAYTYPE_DIRECTORY,
RESOURCEDISPLAYTYPE_TREE,
RESOURCEDISPLAYTYPE_NDSCONTAINER
}
[StructLayout(LayoutKind.Sequential)]
private struct NETRESOURCE
{
public ResourceScope oResourceScope;
public ResourceType oResourceType;
public ResourceDisplayType oDisplayType;
public ResourceUsage oResourceUsage;
public string sLocalName;
public string sRemoteName;
public string sComments;
public string sProvider;
}
[DllImport(" mpr.dll" )]
private static extern int WNetAddConnection2
(ref NETRESOURCE oNetwork Resource, string sPassword,
string sUserName, int iFlags);
[DllImport(" mpr.dll" )]
private static extern int WNetCancelConnection2
(string sLocalName, uint iFlags, int iForce);
public static void Map Network Drive (string sDrive Letter, string sNetwork Path)
{
if (sNetwork Path.Substring(sNetwork Path.Length - 1 , 1 ) == @" \" )
{
sNetwork Path = sNetwork Path.Substring(0 , sNetwork Path.Length - 1 );
}
NETRESOURCE oNetwork Resource = new NETRESOURCE();
oNetwork Resource.oResourceType = ResourceType.RESOURCETYPE_DISK;
oNetwork Resource.sLocalName = sDrive Letter + " :" ;
oNetwork Resource.sRemoteName = sNetwork Path;
if (IsDrive Map ped(sDrive Letter))
{
DisconnectNetwork Drive (sDrive Letter, true );
}
WNetAddConnection2(ref oNetwork Resource, null , null , 0 );
}
public static int DisconnectNetwork Drive (string sDrive Letter, bool bForceDisconnect)
{
if (bForceDisconnect)
{
return WNetCancelConnection2(sDrive Letter + " :" , 0 , 1 );
}
else
{
return WNetCancelConnection2(sDrive Letter + " :" , 0 , 0 );
}
}
public static bool IsDrive Map ped(string sDrive Letter)
{
string [] Drive List = Environment.GetLogicalDrive s();
for (int i = 0 ; i < Drive List.Length; i++)
{
if (sDrive Letter + " :\\" == Drive List[i].ToString())
{
return true;
}
}
return false;
}
}