1 public enum ERROR_ID
2 {
3 ERROR_SUCCESS = 0, // Success
4 ERROR_BUSY = 170,
5 ERROR_MORE_DATA = 234,
6 ERROR_NO_BROWSER_SERVERS_FOUND = 6118,
7 ERROR_INVALID_LEVEL = 124,
8 ERROR_ACCESS_DENIED = 5,
9 ERROR_INVALID_PASSWORD = 86,
10 ERROR_INVALID_PARAMETER = 87,
11 ERROR_BAD_DEV_TYPE = 66,
12 ERROR_NOT_ENOUGH_MEMORY = 8,
13 ERROR_NETWORK_BUSY = 54,
14 ERROR_BAD_NETPATH = 53,
15 ERROR_NO_NETWORK = 1222,
16 ERROR_INVALID_HANDLE_STATE = 1609,
17 ERROR_EXTENDED_ERROR = 1208,
18 ERROR_DEVICE_ALREADY_REMEMBERED = 1202,
19 ERROR_NO_NET_OR_BAD_PATH = 1203
20 }
21
22 public enum RESOURCE_SCOPE
23 {
24 RESOURCE_CONNECTED = 1,
25 RESOURCE_GLOBALNET = 2,
26 RESOURCE_REMEMBERED = 3,
27 RESOURCE_RECENT = 4,
28 RESOURCE_CONTEXT = 5
29 }
30
31 public enum RESOURCE_TYPE
32 {
33 RESOURCETYPE_ANY = 0,
34 RESOURCETYPE_DISK = 1,
35 RESOURCETYPE_PRINT = 2,
36 RESOURCETYPE_RESERVED = 8,
37 }
38
39 public enum RESOURCE_USAGE
40 {
41 RESOURCEUSAGE_CONNECTABLE = 1,
42 RESOURCEUSAGE_CONTAINER = 2,
43 RESOURCEUSAGE_NOLOCALDEVICE = 4,
44 RESOURCEUSAGE_SIBLING = 8,
45 RESOURCEUSAGE_ATTACHED = 16,
46 RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
47 }
48
49 public enum RESOURCE_DISPLAYTYPE
50 {
51 RESOURCEDISPLAYTYPE_GENERIC = 0,
52 RESOURCEDISPLAYTYPE_DOMAIN = 1,
53 RESOURCEDISPLAYTYPE_SERVER = 2,
54 RESOURCEDISPLAYTYPE_SHARE = 3,
55 RESOURCEDISPLAYTYPE_FILE = 4,
56 RESOURCEDISPLAYTYPE_GROUP = 5,
57 RESOURCEDISPLAYTYPE_NETWORK = 6,
58 RESOURCEDISPLAYTYPE_ROOT = 7,
59 RESOURCEDISPLAYTYPE_SHAREADMIN = 8,
60 RESOURCEDISPLAYTYPE_DIRECTORY = 9,
61 RESOURCEDISPLAYTYPE_TREE = 10,
62 RESOURCEDISPLAYTYPE_NDSCONTAINER = 11
63 }
64
65 [StructLayout(LayoutKind.Sequential)]
66 public struct NETRESOURCE
67 {
68 public RESOURCE_SCOPE dwScope;
69 public RESOURCE_TYPE dwType;
70 public RESOURCE_DISPLAYTYPE dwDisplayType;
71 public RESOURCE_USAGE dwUsage;
72
73 [MarshalAs(UnmanagedType.LPStr)]
74 public string lpLocalName;
75
76 [MarshalAs(UnmanagedType.LPStr)]
77 public string lpRemoteName;
78
79 [MarshalAs(UnmanagedType.LPStr)]
80 public string lpComment;
81
82 [MarshalAs(UnmanagedType.LPStr)]
83 public string lpProvider;
84 }
85
86 public class NetworkConnection
87 {
88 [DllImport("mpr.dll")]
89 public static extern int WNetAddConnection2A(NETRESOURCE[] lpNetResource, string lpPassword, string lpUserName, int dwFlags);
90
91 [DllImport("mpr.dll")]
92 public static extern int WNetCancelConnection2A(string sharename, int dwFlags, int fForce);
93
94 public static int Connect(string remotePath, string localPath, string username, string password)
95 {
96 NETRESOURCE[] share_driver = new NETRESOURCE[1];
97 share_driver[0].dwScope = RESOURCE_SCOPE.RESOURCE_GLOBALNET;
98 share_driver[0].dwType = RESOURCE_TYPE.RESOURCETYPE_DISK;
99 share_driver[0].dwDisplayType = RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_SHARE;
100 share_driver[0].dwUsage = RESOURCE_USAGE.RESOURCEUSAGE_CONNECTABLE;
101 share_driver[0].lpLocalName = localPath;
102 share_driver[0].lpRemoteName = remotePath;
103
104 Disconnect(localPath);
105 int ret = WNetAddConnection2A(share_driver, password, username, 1);
106
107 return ret;
108 }
109
110 public static int Disconnect(string localpath)
111 {
112 return WNetCancelConnection2A(localpath, 1, 1);
113 }
114 }