Для написания утилиты на C#, которая должна подключать пользователю сетевые диски, возникла нестандартная задача, поскольку в .Net отсутствует возможность работы с сетевыми ресурсами. В процессе поиска решения была найдена информация о применении функций из набора Win32 API — WNetAddConnection2A для подключения и WNetCancelConnection2A для отключения дисков. Ниже приведен фрагмент кода, позволяющий решить данную задачу:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
using System.Runtime.InteropServices; public class NetworkDrive { 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() { oResourceType = ResourceType.RESOURCETYPE_DISK, sLocalName = sDriveLetter + ":", 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; } } |
[…]