C# 映射网络驱动器(Win API)

 

1 // 资源类,用于记录映射类型和设置
2   [StructLayout(LayoutKind.Sequential)]
3 public class NETRESOURCE
4 {
5 public int dwScope; // 只能取2
6   public int dwType; // 0为打印机或驱动器,1为驱动器,2为打印机
7   public int dwDisplayType; // 取0,自动设置
8   public int dwUsage; // 取1
9 public string LocalName; // 本地盘符或名称
10 public string RemoteName; // 远程地址
11 public string Comment; // NULL即可,A pointer to a NULL-terminated string that contains a comment supplied by the network provider.
12 public string Provider; // NULL即可,A pointer to a NULL-terminated string that contains the name of the provider that owns the resource. This member can be NULL if the provider name is unknown.
13 }
14
15 // 控制(主)类,创建、删除映射
16 public class NetDriveCtl
17 {
18 ArrayList NDList;
19
20 public NetDriveCtl()
21 {
22 NDList = new ArrayList();
23 }
24
25 public string CreateDrive( string LocalName, string RemoteName, string UserName, string Password)
26 {
27 NETRESOURCE NetDrive = new NETRESOURCE();
28 NetDrive.dwScope = 2 ;
29 NetDrive.dwType = 0 ;
30 NetDrive.dwDisplayType = 0 ;
31 NetDrive.dwUsage = 1 ;
32 NetDrive.LocalName = LocalName;
33 NetDrive.RemoteName = RemoteName;
34
35 NDList.Add(NetDrive);
36 return ConnectDrive(NetDrive, UserName, Password);
37 }
38
39 public Boolean DeleteDrive( string LocalName, string RemoteName)
40 {
41 foreach (NETRESOURCE NetDrive in NDList)
42 {
43 if ((NetDrive.LocalName == LocalName) && (NetDrive.RemoteName == RemoteName))
44 {
45 DisconnectDrive(NetDrive);
46 NDList.Remove(NetDrive);
47 return true ;
48 }
49 }
50 return false ;
51 }
52
53 private string ConnectDrive(NETRESOURCE NetDrive, string UserName, string Password)
54 {
55 StringBuilder UN = new StringBuilder(UserName);
56 StringBuilder PW = new StringBuilder(Password);
57
58 return WNetAddConnection2(NetDrive, PW, UN, 0 ).ToString();
59 }
60
61 private string DisconnectDrive(NETRESOURCE NetDrive)
62 {
63 string LocalName = NetDrive.LocalName;
64 return WNetCancelConnection2(LocalName, 1 , true ).ToString();
65 }
66
67 private string DisconnectDrive( string LocalName)
68 {
69 return WNetCancelConnection2(LocalName, 1 , true ).ToString();
70 }
71
72 // 这两个是系统API函数
73 [DllImport( " mpr.dll " , EntryPoint = " WNetAddConnection2 " )]
74 private static extern uint WNetAddConnection2([In] NETRESOURCE lpNetResource, StringBuilder lpPassword, StringBuilder lpUsername, uint dwFlags);
75 [DllImport( " Mpr.dll " )]
76 private static extern uint WNetCancelConnection2( string lpName, uint dwFlags, bool fForce);
77 }

 

调用示例:

 

NetDriveCtl ndc = new NetDriveCtl();
ndc.CreateDrive(
" T: " , @" \\1.1.1.1\V$ " , "密码 " , " 用户名 " );
ndc.CreateDrive(
" T: " , @" \\1.1.1.1\V$ " ,"密码","用户名");

 

posted on 2010-12-29 17:31 MyBox 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/jinzm/archive/2010/12/29/1920910.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值