using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.NetworkInformation; namespace MESNetworkAnalyser { public partial class MainForm : Form { private string[] computerList; BackgroundWorker bw = new BackgroundWorker(); public MainForm() { InitializeComponent(); bw.WorkerSupportsCancellation = true; bw.WorkerReportsProgress = true; } private void MainForm_Load(object sender, EventArgs e) { computerList = Read_Computer_List("computerList.txt");
int inc = 0; foreach (string computer in computerList) { computerList.SetValue(computer.Trim(),inc); inc++; } ProgressText.Lines = computerList; } private void bw_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker;
} private string[] Read_Computer_List(string fileName) { StreamReader sr = new StreamReader(fileName); string fileContents = sr.ReadToEnd(); string[] fileLines = fileContents.Split(new char[] {'\n'}); return fileLines; } private string ping(string computerName) { computerName.Trim(); Ping pingSender = new Ping(); PingOptions options = new PingOptions(); // Use the default Ttl value which is 128, // but change the fragmentation behavior. options.DontFragment = true; // Create a buffer of 32 bytes of data to be transmitted. string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; byte[] buffer = Encoding.ASCII.GetBytes(data); int timeout = 120; try { PingReply reply = pingSender.Send(computerName, timeout, buffer, options); if (reply.Status == IPStatus.Success) { return string.Concat("RoundTrip time: ", reply.RoundtripTime); } else { return "No Connection"; } } catch { return "No Connection"; } } private void StartButton_Click(object sender, EventArgs e) { int inc = 0; int listSize = computerList.Length; int successCounter = 0; foreach (string computer in computerList) { string pingReply = ping(computer); string[] tempArray = ProgressText.Lines; tempArray[inc] = String.Concat(computer, "...", pingReply); ProgressText.Lines = tempArray; if (!pingReply.Equals("No Connection")) { successCounter++; } inc++; totalProgressBar.Value = inc / listSize * 100; Thread.Sleep(1000); } ResultsText.Text = String.Concat(successCounter, "/", listSize, " computers were found on the network"); } } }
|