-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
61 lines (52 loc) · 1.75 KB
/
MainWindow.xaml.cs
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
using System;
using System.ComponentModel;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using System.Windows;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
namespace Pinger
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
listHosts.Items.SortDescriptions.Add(new SortDescription("", ListSortDirection.Ascending));
listHosts.Items.IsLiveSorting = true;
}
private async void BtnScan_OnClick(object sender, RoutedEventArgs e)
{
var range = txtIp.Text + cbCidr.Text;
var scanner = new Scanner(range);
var scans = scanner.Scan();
ObservableCollection<Host> hosts = new ObservableCollection<Host>();
listHosts.ItemsSource = hosts;
hosts.Clear();
while (scans.Count > 0)
{
var scan = await Task.WhenAny(scans);
var result = await scan;
scans.Remove(scan);
if (result.Ping.Status == IPStatus.Success)
{
hosts.Add(new Host() { IP = result.IpAddress.ToString(), DNS = result.Dns ?? "Unknown", PING = result.Ping.RoundtripTime.ToString() + "ms"});
}
}
}
public class Host : IComparable
{
public string IP { get; set; }
public string DNS { get; set; }
public string PING { get; set; }
public int CompareTo(object obj)
{
return 0;
}
}
}
}