Пример #1
0
        private void DataLoad()
        {
            try
            {
                serverTimers = new List <System.Threading.Timer>();
                Dictionary <String, IPAddress> networkcards = GetNetworkAdapters();
                var NICNames = (from cards in networkcards select cards.Key).ToArray();
                DataGridViewComboBoxColumn networkcolumn = grdServerList.Columns[1] as DataGridViewComboBoxColumn;
                networkcolumn.DataSource = NICNames;
                NetworkInterface[] NICCards = NetworkInterface.GetAllNetworkInterfaces();
                if (File.Exists(serverDataPath))
                {
                    serverList = serverdata.readData(serverDataPath);
                }
                Classes.Ping ping = new Classes.Ping();
                grdServerList.Rows.Add(null, null, null);

                int i = 0;
                if (serverList != null)
                {
                    foreach (Server item in serverList)
                    {
                        string pingfrom = GetLocalIPv4(NetworkInterfaceType.Wireless80211);
                        //Callback_data.rowID = i;
                        DataGridViewRow newRow = new DataGridViewRow();
                        newRow = (DataGridViewRow)grdServerList.Rows[0].Clone();
                        newRow.Cells[0].Value = item.Name;
                        newRow.Cells[2].Value = item.Comments;
                        ((DataGridViewComboBoxCell)newRow.Cells[1]).Value = item.NIC;
                        grdServerList.Rows.Add(newRow);
                        serverTimers.Add(new System.Threading.Timer(timerCallBack, i, 1000, pingfreq * 1000));
                        serverTimers.Add(new System.Threading.Timer(PingCallback, i, 1000, pingfreq * 1000));
                        i++;
                    }
                }

                grdServerList.Rows.RemoveAt(0);
            }
            catch (Exception ex)
            {
            }
        }
Пример #2
0
        protected void timerCallBack(object data)
        {
            try
            {
                int    rowID      = Convert.ToInt32(data);
                Server serverItem = serverList[rowID];

                IPAddress sourceIP = IPAddress.Loopback;
                IPAddress destIP   = IPAddress.Parse(serverItem.Name);

                NetworkInterface[] cards = NetworkInterface.GetAllNetworkInterfaces();

                foreach (NetworkInterface card in cards)
                {
                    String cardName = card.Description;

                    if (cardName == serverItem.NIC)
                    {
                        foreach (UnicastIPAddressInformation ip in card.GetIPProperties().UnicastAddresses)
                        {
                            if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                            {
                                sourceIP = ip.Address;
                            }
                        }
                    }
                }

                bool         pinged  = Classes.IcmpPing.Send(sourceIP, destIP).Status.ToString() == "Success";
                Classes.Ping ping    = new Classes.Ping();
                Log          logger  = new Log();
                string       localIP = GetLocalIPv4(NetworkInterfaceType.Wireless80211);
                pinged = ping.Send(localIP, serverList[rowID].Name);
                if (pinged)
                {
                    logger.AppendToFile("Successfully pinged " +
                                        serverList[rowID].Name +
                                        " during " +
                                        DateTime.Now.TimeOfDay +
                                        " on " + DateTime.Now.Date +
                                        " using " + serverList[rowID].NIC +
                                        Environment.NewLine,
                                        logger.path);
                }
                else
                {
                    logger.AppendToFile("Failed to ping " +
                                        serverList[rowID].Name +
                                        " during " +
                                        DateTime.Now.TimeOfDay +
                                        " on " + DateTime.Now.Date +
                                        " using " + serverList[rowID].NIC +
                                        Environment.NewLine,
                                        logger.path);
                }
            }
            catch (DirectoryNotFoundException ex)
            {
                MessageBox.Show("Error: Directory does not exist:" +
                                Environment.NewLine +
                                Properties.Settings.Default.logfilepath);
            }
        }