/// <summary> /// Enumerates network resources. /// </summary> /// <param name="remoteName">The name of the server</param> /// <returns>Array of NetworkResource class</returns> public static NetworkResource[] GetNetworkResources(string remoteName) { NETRESOURCE netRes = new NETRESOURCE(); netRes.dwScope = RESOURCE_GLOBALNET; netRes.dwType = RESOURCETYPE_DISK; netRes.dwUsage = RESOURCEUSAGE_CONTAINER; netRes.lpRemoteName = MarshalEx.StringToHGlobalUni(remoteName); netRes.lpLocalName = MarshalEx.StringToHGlobalUni(""); netRes.lpComment = IntPtr.Zero; netRes.lpProvider = IntPtr.Zero; IntPtr hEnum = IntPtr.Zero; int ret = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, netRes, ref hEnum); if (ret != 0) { throw new System.ComponentModel.Win32Exception(ret, ((NetworkErrors)ret).ToString()); } //Allocate memory for NETRESOURCE array int bufferSize = 16384; IntPtr buffer = MarshalEx.AllocHLocal(bufferSize); if (buffer == IntPtr.Zero) { throw new OutOfMemoryException("There's not enough native memory."); } uint c = 0xFFFFFFFF; int count = (int)c; int size = Marshal.SizeOf(typeof(NETRESOURCE)); ArrayList arrList = new ArrayList(); ret = WNetEnumResource(hEnum, ref count, buffer, ref bufferSize); if (ret == 0) { IntPtr currPtr = buffer; for(int i=0;i<count;i++) { netRes = (NETRESOURCE)Marshal.PtrToStructure(currPtr, typeof(NETRESOURCE)); NetworkResource res = new NetworkResource("", Marshal.PtrToStringUni(netRes.lpRemoteName)); //res.RemoteName = Marshal.PtrToStringUni(netRes.lpRemoteName); arrList.Add(res); currPtr = new IntPtr((int)currPtr + size); } } else { //clean up MarshalEx.FreeHLocal(buffer); throw new System.ComponentModel.Win32Exception(ret, ((NetworkErrors)ret).ToString()); } //clean up MarshalEx.FreeHLocal(buffer); return (NetworkResource[])arrList.ToArray(typeof(NetworkResource)); }
/// <summary> /// Enumerates and returns all connected network resources. /// </summary> /// <returns>Array of NetworkResource class</returns> public static NetworkResource[] GetConnectedResources() { NETRESOURCE netRes = new NETRESOURCE(); IntPtr hEnum = IntPtr.Zero; int ret = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, 0, IntPtr.Zero, ref hEnum); if (ret != 0) { throw new System.ComponentModel.Win32Exception(ret, ((NetworkErrors)ret).ToString()); } //Allocate memory for NETRESOURCE array int bufferSize = 16384; IntPtr buffer = MarshalEx.AllocHLocal(bufferSize); if (buffer == IntPtr.Zero) { throw new OutOfMemoryException("There's not enough native memory."); } uint c = 0xFFFFFFFF; int count = (int)c; int size = Marshal.SizeOf(typeof(NETRESOURCE)); ArrayList arrList = new ArrayList(); ret = WNetEnumResource(hEnum, ref count, buffer, ref bufferSize); if (ret == 0) { IntPtr currPtr = buffer; for(int i=0;i<count;i++) { netRes = (NETRESOURCE)Marshal.PtrToStructure(currPtr, typeof(NETRESOURCE)); NetworkResource res = new NetworkResource(Marshal.PtrToStringUni(netRes.lpLocalName), Marshal.PtrToStringUni(netRes.lpRemoteName)); //res.RemoteName = Marshal.PtrToStringUni(netRes.lpRemoteName); //res.ShareName = Marshal.PtrToStringUni(netRes.lpLocalName); arrList.Add(res); currPtr = new IntPtr((int)currPtr + size); } } else { //clean up MarshalEx.FreeHLocal(buffer); } //clean up MarshalEx.FreeHLocal(buffer); return (NetworkResource[])arrList.ToArray(typeof(NetworkResource)); }
/// <summary> /// Enumerates network resources. /// </summary> /// <param name="remoteName">The name of the server</param> /// <returns>Array of NetworkResource class</returns> public static NetworkResource[] GetNetworkResources(string remoteName) { NETRESOURCE netRes = new NETRESOURCE(); netRes.dwScope = RESOURCE_GLOBALNET; netRes.dwType = RESOURCETYPE_DISK; netRes.dwUsage = RESOURCEUSAGE_CONTAINER; netRes.lpRemoteName = Marshal2.StringToHGlobalUni(remoteName); netRes.lpLocalName = Marshal2.StringToHGlobalUni(""); netRes.lpComment = IntPtr.Zero; netRes.lpProvider = IntPtr.Zero; IntPtr hEnum = IntPtr.Zero; int ret = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, netRes, ref hEnum); if (ret != 0) { throw new System.ComponentModel.Win32Exception(ret, ((NetworkErrors)ret).ToString()); } //Allocate memory for NETRESOURCE array int bufferSize = 16384; IntPtr buffer = Marshal.AllocHGlobal(bufferSize); OpenNETCF.Runtime.InteropServices.Marshal2.SetMemory(buffer, 0, bufferSize, false); if (buffer == IntPtr.Zero) { throw new OutOfMemoryException("There's not enough native memory."); } uint c = 0xFFFFFFFF; int count = (int)c; int size = Marshal.SizeOf(typeof(NETRESOURCE)); ArrayList arrList = new ArrayList(); ret = WNetEnumResource(hEnum, ref count, buffer, ref bufferSize); if (ret == 0) { IntPtr currPtr = buffer; for (int i = 0; i < count; i++) { netRes = (NETRESOURCE)Marshal.PtrToStructure(currPtr, typeof(NETRESOURCE)); NetworkResource res = new NetworkResource("", Marshal.PtrToStringUni(netRes.lpRemoteName)); //res.RemoteName = Marshal.PtrToStringUni(netRes.lpRemoteName); arrList.Add(res); currPtr = new IntPtr((int)currPtr + size); } } else { //clean up Marshal.FreeHGlobal(buffer); throw new System.ComponentModel.Win32Exception(ret, ((NetworkErrors)ret).ToString()); } //clean up Marshal.FreeHGlobal(buffer); return((NetworkResource[])arrList.ToArray(typeof(NetworkResource))); }