Beispiel #1
0
        /// <summary>
        /// Copies the object.
        /// </summary>
        /// <returns></returns>
        public NetworkMachine Copy()
        {
            NetworkMachine nm = new NetworkMachine();

            nm.Name      = this.Name;
            nm.IPAddress = this.IPAddress;
            return(nm);
        }
Beispiel #2
0
        public bool Equals(NetworkMachine other)
        {
            if (other == null)
            {
                return(false);
            }

            return(this.GetHashCode() == other.GetHashCode());
        }
Beispiel #3
0
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }

            NetworkMachine other = obj as NetworkMachine;

            return(this.Equals(other));
        }
Beispiel #4
0
        /// <summary>
        /// Gets the computers in the local network.
        /// </summary>
        /// <returns>The computers in the local network.</returns>
        /// <remarks>To find computers in the local network, this method uses the Active Directory Domain Services.</remarks>
        public static NetworkMachine[] GetComputersInLocalNetwork()
        {
            List <NetworkMachine> networkMachines = new List <NetworkMachine>();
            DirectoryEntry        root            = new DirectoryEntry("WinNT:");

            foreach (DirectoryEntry computers in root.Children)
            {
                foreach (DirectoryEntry computer in computers.Children)
                {
                    if (computer.Name != "Schema")
                    {
                        var networkMachine = new NetworkMachine();
                        networkMachine.Name      = computer.Name;
                        networkMachine.IPAddress = GetIP4AddressByHostname(computer.Name);
                        networkMachines.Add(networkMachine);
                    }
                }
            }

            return(networkMachines.ToArray());
        }
Beispiel #5
0
        /// <summary>
        /// Gets all network devices in a local network by IP dress range.
        /// </summary>
        /// <param name="ipAddressRange">The IP address range to scan.</param>
        /// <returns>A NetworkMachine array containing all network devices active in the local network.</returns>
        public static NetworkMachine[] GetNetworkMachinesInLocalNetworkFromIPAddressRange(IPAddress[] ipAddressRange, CancellationToken cancellationToken)
        {
            // Find active machines first, because Dns.GetHostEntry will last longer.
            ConcurrentBag <IPAddress> alive = new ConcurrentBag <IPAddress>();
            List <Task> tasks = new List <Task>();

            foreach (var ip in ipAddressRange)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }

                var ipCopy = ip; // Work with a copy.
                var task   = Task.Factory.StartNew(() =>
                {
                    var ping = new Ping();

                    if (ping.Send(ipCopy, 200).Status != IPStatus.TimedOut)
                    {
                        alive.Add(ip);
                    }
                });

                tasks.Add(task);
            }

            try
            {
                Task.WaitAll(tasks.ToArray(), cancellationToken);
            }
            catch (OperationCanceledException)
            {
                return(null);
            }

            ipAddressRange = alive.ToArray();
            ConcurrentBag <NetworkMachine> networkMachines = new ConcurrentBag <NetworkMachine>();

            tasks = new List <Task>();

            foreach (var ip in ipAddressRange)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }

                var ipCopy = ip; // Work with a copy.
                var task   = Task.Factory.StartNew(() =>
                {
                    try
                    {
                        var hostEntry           = Dns.GetHostEntry(ip);
                        var networkMachine      = new NetworkMachine();
                        networkMachine.Name     = hostEntry.HostName;
                        IPAddress[] ip4Adresses = Array.FindAll(hostEntry.AddressList, a => a.AddressFamily == AddressFamily.InterNetwork);

                        if (ip4Adresses != null && ip4Adresses.Length > 0)
                        {
                            networkMachine.IPAddress = ip4Adresses[0];
                        }

                        networkMachines.Add(networkMachine);
                    }
                    catch (SocketException)
                    {
                    }
                });

                tasks.Add(task);
            }

            try
            {
                Task.WaitAll(tasks.ToArray(), cancellationToken);
            }
            catch (OperationCanceledException)
            {
                return(null);
            }

            return(networkMachines.ToArray());
        }