Map Network Driver

WNetAddConnection2 is on c++
public class DriveSettings
{
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 oNetworkResource, string sPassword, 
	string sUserName, int iFlags);

[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2
	(string sLocalName, uint iFlags, int iForce);

public static void MapNetworkDrive(string sDriveLetter, string sNetworkPath)
{
 //Checks if the last character is \ as this causes error on mapping a drive.
 if (sNetworkPath.Substring(sNetworkPath.Length - 1, 1) == @"\")
 {
 sNetworkPath = sNetworkPath.Substring(0, sNetworkPath.Length - 1);
 }

 NETRESOURCE oNetworkResource = new NETRESOURCE();
 oNetworkResource.oResourceType = ResourceType.RESOURCETYPE_DISK;
 oNetworkResource.sLocalName = sDriveLetter + ":";
 oNetworkResource.sRemoteName = sNetworkPath;

 //If Drive is already mapped disconnect the current 
 //mapping before adding the new mapping
 if (IsDriveMapped(sDriveLetter))
 {
 DisconnectNetworkDrive(sDriveLetter, true);
 }

 WNetAddConnection2(ref oNetworkResource, null, null, 0);
}

public static int DisconnectNetworkDrive(string sDriveLetter, bool bForceDisconnect)
{
 if (bForceDisconnect)
 {
 return WNetCancelConnection2(sDriveLetter + ":", 0, 1);
 }
 else
 {
 return WNetCancelConnection2(sDriveLetter + ":", 0, 0);
 }
}

public static bool IsDriveMapped(string sDriveLetter)
{
 string[] DriveList = Environment.GetLogicalDrives();
 for (int i = 0; i < DriveList.Length; i++)
 {
 if (sDriveLetter + ":\\" == DriveList[i].ToString())
 {
 return true;
 }
 }
 return false;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值