private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (thread_StatusStrip != null) { thread_StatusStrip.Abort(); } if (thread_ScanMonitor != null) { thread_ScanMonitor.Abort(); } if (thread_StartScan != null) { thread_StartScan.Abort(); } StreamWriter sw = new StreamWriter("List.txt", false); foreach (var key in IPEntities.Keys) { IPEntity ipEntity = IPEntities[key]; if (ipEntity != null) { sw.WriteLine(ipEntity.ToString()); } } sw.Close(); }
private void button2_Click(object sender, EventArgs e) { Dictionary <string, IPEntity> tempIPList = new Dictionary <string, IPEntity>(); foreach (var key in IPEntities.Keys) { if (IPEntities[key] != null) { tempIPList.Add(key, IPEntities[key]); } } DataGridViewRow[] dataGridViewRows = new DataGridViewRow[tempIPList.Count]; int i = 0; foreach (var key in tempIPList.Keys) { IPEntity ipEntity = tempIPList[key]; textBox1.Text += ipEntity.ToString() + "\r\n"; dataGridViewRows[i] = new DataGridViewRow(); dataGridViewRows[i].CreateCells(dataGridView1); dataGridViewRows[i].Cells[0].Value = ipEntity.ComputerName; dataGridViewRows[i].Cells[1].Value = ipEntity.IPAddr; dataGridViewRows[i].Cells[2].Value = ipEntity.MacAddr; dataGridViewRows[i].Cells[3].Value = ipEntity.Status; dataGridViewRows[i].Cells[4].Value = ipEntity.LastOnline; i++; } dataGridView1.Rows.AddRange(dataGridViewRows); }
private void button1_Click(object sender, EventArgs e) { IPEntity iPEntity = new IPEntity("Frank|192.168.1.1|00:00:00:00:00|true|2019-01-08 11:09:21"); textBox1.Text = iPEntity.ToString(); thread_StartScan = new Thread(RunThreadPool); thread_StartScan.Start(); }
private void UpdateDataGridView(object obj) { if (this.InvokeRequired) { this.Invoke(new UpdateDataGridViewCallBack(UpdateDataGridView), obj); } else { Dictionary <string, IPEntity> tempIPList = new Dictionary <string, IPEntity>(); foreach (var key in IPEntities.Keys) { IPEntity ipEntity = IPEntities[key]; if (ipEntity != null) { if (ipEntity.ComputerName != "" && ipEntity.MacAddr != "00-00-00-00-00-00") { DateTime dt1 = ipEntity.LastOnline; DateTime dt2 = DateTime.Now; TimeSpan ts = dt2 - dt1; if (!(ts.TotalSeconds > 60 * 60 * 24 * 10)) { tempIPList.Add(key, ipEntity); } } } } dataGridView1.Rows.Clear(); DataGridViewRow[] dataGridViewRows = new DataGridViewRow[tempIPList.Count]; int i = 0; foreach (var key in tempIPList.Keys) { IPEntity ipEntity = tempIPList[key]; textBox1.Text += ipEntity.ToString() + "\r\n"; dataGridViewRows[i] = new DataGridViewRow(); dataGridViewRows[i].CreateCells(dataGridView1); dataGridViewRows[i].Cells[0].Value = ipEntity.ComputerName; dataGridViewRows[i].Cells[1].Value = ipEntity.IPAddr; dataGridViewRows[i].Cells[2].Value = ipEntity.MacAddr; dataGridViewRows[i].Cells[3].Value = ipEntity.Status; dataGridViewRows[i].Cells[4].Value = ipEntity.LastOnline; i++; } dataGridView1.Rows.AddRange(dataGridViewRows); } }
private void button3_Click(object sender, EventArgs e) { Dictionary <string, IPEntity> tempIPList = new Dictionary <string, IPEntity>(); tempIPList.Add("10.10.64.39", null); tempIPList.Add("10.10.64.157", null); tempIPList.Add("10.10.64.172", null); foreach (var key in tempIPList.Keys) { IPEntity ipEntity = new IPEntity(key, DateTime.Now); ipEntity.ComputerName = ipEntity.GetHostName(key); ipEntity.MacAddr = ipEntity.GetMacBySendARP(key); textBox1.Text += ipEntity.ToString() + "\r\n"; } }
public void ThreadPoolMethod(object obj) { //TODO: Add your code here object[] objs = (object[])obj; int index = (int)objs[0]; string IPAddr = IPList[index]; IPEntity ipEntity = IPEntities[IPAddr]; if (ipEntity == null) { ipEntity = new IPEntity(IPAddr, DateTime.Now); ipEntity.ComputerName = ipEntity.GetHostName(IPAddr); ipEntity.MacAddr = ipEntity.GetMacBySendARP(IPAddr); if (ipEntity.ComputerName != "" && ipEntity.MacAddr != "00-00-00-00-00-00") { ipEntity.Status = true; } IPEntities[IPAddr] = ipEntity; } else { ipEntity.ComputerName = ipEntity.GetHostName(IPAddr); ipEntity.MacAddr = ipEntity.GetMacBySendARP(IPAddr); if (ipEntity.ComputerName != "" && ipEntity.MacAddr != "00-00-00-00-00-00") { ipEntity.Status = true; ipEntity.LastOnline = DateTime.Now; } else { ipEntity.Status = false; } IPEntities[IPAddr] = ipEntity; } //UpdateUI((int)objs[0]); ManualResetEvent e = (ManualResetEvent)objs[1]; e.Set(); }
//载入文件 private void LoadConfig() { if (!File.Exists("List.txt")) { return; } StreamReader sr = new StreamReader("List.txt"); string line = ""; while ((line = sr.ReadLine()) != null) { IPEntity ipEntity = new IPEntity(line); if (!IPEntities.ContainsKey(ipEntity.IPAddr)) { IPEntities.Add(ipEntity.IPAddr, ipEntity); IPList.Add(ipEntity.IPAddr); } else { IPEntities[ipEntity.IPAddr] = ipEntity; } } sr.Close(); }