Как получить список компьютеров в сети. - C#
Формулировка задачи:
Доброе время суток. Как получить список компьютеров в сети используя только классы .NET без всякиз API.
Решение задачи: «Как получить список компьютеров в сети.»
textual
Листинг программы
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Collections; namespace PortTest { public class ComputersInLocalNetwork { [DllImport("netapi32.dll", EntryPoint = "NetServerEnum")] public static extern NERR NetServerEnum( [MarshalAs(UnmanagedType.LPWStr)]string ServerName, int Level, out IntPtr BufPtr, int PrefMaxLen, ref int EntriesRead, ref int TotalEntries, SV_101_TYPES ServerType, [MarshalAs(UnmanagedType.LPWStr)] string Domain, int ResumeHandle); [DllImport("netapi32.dll", EntryPoint = "NetApiBufferFree")] public static extern NERR NetApiBufferFree(IntPtr Buffer); [StructLayout(LayoutKind.Sequential)] public struct SERVER_INFO_101 { [MarshalAs(UnmanagedType.U4)] public uint sv101_platform_id; [MarshalAs(UnmanagedType.LPWStr)] public string sv101_name; [MarshalAs(UnmanagedType.U4)] public uint sv101_version_major; [MarshalAs(UnmanagedType.U4)] public uint sv101_version_minor; [MarshalAs(UnmanagedType.U4)] public uint sv101_type; [MarshalAs(UnmanagedType.LPWStr)] public string sv101_comment; } /// <summary> /// список ошибок, возвращаемых NetServerEnum /// </summary> public enum NERR { NERR_Success = 0, // успех ERROR_ACCESS_DENIED = 5, ERROR_NOT_ENOUGH_MEMORY = 8, ERROR_BAD_NETPATH = 53, ERROR_NETWORK_BUSY = 54, ERROR_INVALID_PARAMETER = 87, ERROR_INVALID_LEVEL = 124, ERROR_MORE_DATA = 234, ERROR_EXTENDED_ERROR = 1208, ERROR_NO_NETWORK = 1222, ERROR_INVALID_HANDLE_STATE = 1609, ERROR_NO_BROWSER_SERVERS_FOUND = 6118, } /// <summary> /// Типы серверов /// </summary> [Flags] public enum SV_101_TYPES : uint { SV_TYPE_WORKSTATION = 0x00000001, SV_TYPE_SERVER = 0x00000002, SV_TYPE_SQLSERVER = 0x00000004, SV_TYPE_DOMAIN_CTRL = 0x00000008, SV_TYPE_DOMAIN_BAKCTRL = 0x00000010, SV_TYPE_TIME_SOURCE = 0x00000020, SV_TYPE_AFP = 0x00000040, SV_TYPE_NOVELL = 0x00000080, SV_TYPE_DOMAIN_MEMBER = 0x00000100, SV_TYPE_PRINTQ_SERVER = 0x00000200, SV_TYPE_DIALIN_SERVER = 0x00000400, SV_TYPE_XENIX_SERVER = 0x00000800, SV_TYPE_SERVER_UNIX = SV_TYPE_XENIX_SERVER, SV_TYPE_NT = 0x00001000, SV_TYPE_WFW = 0x00002000, SV_TYPE_SERVER_MFPN = 0x00004000, SV_TYPE_SERVER_NT = 0x00008000, SV_TYPE_POTENTIAL_BROWSER = 0x00010000, SV_TYPE_BACKUP_BROWSER = 0x00020000, SV_TYPE_MASTER_BROWSER = 0x00040000, SV_TYPE_DOMAIN_MASTER = 0x00080000, SV_TYPE_SERVER_OSF = 0x00100000, SV_TYPE_SERVER_VMS = 0x00200000, SV_TYPE_WINDOWS = 0x00400000, SV_TYPE_DFS = 0x00800000, SV_TYPE_CLUSTER_NT = 0x01000000, SV_TYPE_TERMINALSERVER = 0x02000000, SV_TYPE_CLUSTER_VS_NT = 0x04000000, SV_TYPE_DCE = 0x10000000, SV_TYPE_ALTERNATE_XPORT = 0x20000000, SV_TYPE_LOCAL_LIST_ONLY = 0x40000000, SV_TYPE_DOMAIN_ENUM = 0x80000000, SV_TYPE_ALL = 0xFFFFFFFF, } // получим список всех компьюетеров public static List<string> GetServerList(SV_101_TYPES type) { SERVER_INFO_101 si; IntPtr pInfo = IntPtr.Zero; int etriesread = 0; int totalentries = 0; List<string> srvs = new List<string>(); try { NERR err = NetServerEnum(null, 101, out pInfo, -1, ref etriesread, ref totalentries, SV_101_TYPES.SV_TYPE_ALL, null, 0); if ((err == NERR.NERR_Success || err == NERR.ERROR_MORE_DATA) && pInfo != IntPtr.Zero) { int ptr = pInfo.ToInt32(); for (int i = 0; i < etriesread; i++) { si = (SERVER_INFO_101)Marshal.PtrToStructure(new IntPtr(ptr), typeof(SERVER_INFO_101)); srvs.Add(si.sv101_name.ToString()); // добавляем имя сервера в список ptr += Marshal.SizeOf(si); } } } catch (Exception) { /* обработка ошибки нифига не делаем :(*/ } finally { // освобождаем выделенную память if (pInfo != IntPtr.Zero) NetApiBufferFree(pInfo); } return (srvs); } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д