public LANComputerModelView(LocalNetworkComputer computer) { Name = computer.Name; Status = computer.Status.ToString(); IPAddress = computer.IPAddress; Latency = computer.Latency == -1 ? "..." : (computer.Latency >= 1000 ? ">1000ms" : computer.Latency.ToString() + "ms"); ToolTip = string.Format(Application.Current.FindResource("ComputerToolTip").ToString(), Environment.NewLine, Name, IPAddress, Latency); }
public void Resolve(LocalNetworkComputer computer) { string name = computer.Name; string status = computer.Status.ToString(); string ipAddress = computer.IPAddress; string latency = computer.Latency == -1 ? "..." : (computer.Latency >= 1000 ? ">1000ms" : computer.Latency.ToString() + "ms"); string toolTip = string.Format(Application.Current.FindResource("ComputerToolTip").ToString(), Environment.NewLine, Name, IPAddress, Latency); if (Name != name) { Name = name; Notify(() => Name); } if (Status != status) { Status = status; Notify(() => Status); } if (IPAddress != ipAddress) { IPAddress = ipAddress; Notify(() => IPAddress); } if (Latency != latency) { Latency = latency; Notify(() => Latency); } if (ToolTip != toolTip) { ToolTip = toolTip; Notify(() => ToolTip); } }
private void Pinger_PingCompleted(object sender, PingCompletedEventArgs e) { LocalNetworkComputer computer = e.UserState as LocalNetworkComputer; int result = -1; int latency = -1; if (e.Cancelled || e.Error != null) { result = 10; latency = 1000; } else { result = (int)e.Reply.Status; latency = result == 0 ? (int)e.Reply.RoundtripTime : 1000; } computer.Status = result; computer.Latency = latency; if (computer.IPAddress == string.Empty) { computer.IPAddress = e.Reply.Address.ToString(); } }
public void ListLANComputers() { DirectoryEntry root = new DirectoryEntry("WinNT:"); foreach (DirectoryEntry computers in root.Children) { foreach (DirectoryEntry computer in computers.Children) { if (computer.Name != "Schema") { LocalNetworkComputer activeComputer = null; foreach (LocalNetworkComputer item in computerList) { if (item.UID == computer.Path) { activeComputer = item; break; } } if (activeComputer == null) { activeComputer = new LocalNetworkComputer();; computerList.Add(activeComputer); } activeComputer.Name = computer.Name; activeComputer.UID = computer.Path; activeComputer.Updated = true; string ipAddress = string.Empty; try { IPAddress ipv4 = null; IPAddress ipv6 = null; IPHostEntry ipHost = Dns.GetHostEntry(computer.Name); foreach (IPAddress ip in ipHost.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { ipv4 = ip; } else if (ip.AddressFamily == AddressFamily.InterNetworkV6) { ipv6 = ip; } } if (ipv4 != null) { ipAddress = ipv4.ToString(); pinger.SendAsync(ipv4, 1000, activeComputer); } else if (ipv6 != null) { ipAddress = ipv6.ToString(); pinger.SendAsync(ipv6, 1000, activeComputer); } else { pinger.SendAsync(computer.Name, 1000, activeComputer); } } catch { } activeComputer.IPAddress = ipAddress; } } } computerList.RemoveAll(computer => { if (computer.Updated) { computer.Updated = false; return(false); } else { return(true); } }); root.Dispose(); }