Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Net;
- using System.Net.NetworkInformation;
- using System.Reflection;
- using System.Timers;
- using System.Windows.Forms;
- using Microsoft.Win32;
- namespace LoLPing
- {
- internal class Program : Form
- {
- private static void Main(string[] args)
- {
- Application.Run(new Program());
- }
- private NotifyIcon tray;
- private ContextMenu menu;
- private Dictionary<string, string> serverOptions = new Dictionary<string, string>() { { "NA", "riot.ca" }, { "EUW", "riot.de" }, { "EUNE", "riot.de" } };
- private int[] alertOptions = new int[] { 100, 200, 300, 400, 500 };
- private MenuItem[] servers;
- private MenuItem[] alerts;
- private System.Timers.Timer timer;
- private bool runWithWindows;
- private RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
- public Program()
- {
- tray = new NotifyIcon();
- tray.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
- runWithWindows = (rk.GetValue("LoLPing") != null) ? true : false;
- menu = new System.Windows.Forms.ContextMenu();
- menu.MenuItems.Add("Ping Now", new EventHandler(pingNowHandler));
- menu.MenuItems.Add("Start with Windows", new EventHandler(startWithWindows));
- menu.MenuItems[1].Checked = runWithWindows;
- servers = new MenuItem[serverOptions.Count];
- string defaultServer = Properties.Settings.Default.Server;
- for (int i = 0; i < serverOptions.Count; i++)
- {
- servers[i] = new MenuItem(serverOptions.Keys.ToArray()[i], new EventHandler(setServer));
- servers[i].RadioCheck = true;
- if (defaultServer == serverOptions.Keys.ToArray()[i]) servers[i].Checked = true;
- }
- menu.MenuItems.Add("Server", servers);
- alerts = new MenuItem[alertOptions.Length];
- for (int i = 0; i < alertOptions.Length; i++)
- {
- alerts[i] = new MenuItem(alertOptions[i].ToString(), new EventHandler(setAlert));
- alerts[i].RadioCheck = true;
- if (Properties.Settings.Default.AlertWhen == alertOptions[i]) alerts[i].Checked = true;
- }
- menu.MenuItems.Add("Alert", alerts);
- menu.MenuItems.Add("Close app", new EventHandler(Exit));
- tray.ContextMenu = menu;
- tray.Visible = true;
- timer = new System.Timers.Timer(60000);
- timer.Elapsed += new ElapsedEventHandler(checkPing);
- timer.Start();
- tray.ShowBalloonTip(3000, "LoLPing", "LoLPing is now running", ToolTipIcon.None);
- }
- protected override void OnLoad(EventArgs e)
- {
- Visible = false;
- ShowInTaskbar = false;
- base.OnLoad(e);
- }
- private void pingNowHandler(object sender, EventArgs e)
- {
- pingNow();
- }
- private void pingNow()
- {
- Ping ping = new Ping();
- PingOptions opt = new PingOptions(128, true);
- byte[] buffer = new byte[32];
- string srv = Properties.Settings.Default.Server;
- string loldns = serverOptions[srv];
- IPAddress ipa = Dns.GetHostAddresses(loldns).First();
- PingReply rpl = ping.Send(ipa, 1000, buffer, opt);
- switch (rpl.Status)
- {
- case IPStatus.Success: showBalloon("Your ping to the " + srv + "-server is " + rpl.RoundtripTime + "ms"); break;
- case IPStatus.TimedOut: showBalloon("Connection to the " + srv + "-server has timed out - You should NOT start a game now"); break;
- default: showBalloon("Ping failed: " + rpl.Status.ToString()); break;
- }
- }
- private void checkPing(object sender, ElapsedEventArgs e)
- {
- Ping ping = new Ping();
- PingOptions opt = new PingOptions(128, true);
- byte[] buffer = new byte[32];
- string srv = Properties.Settings.Default.Server;
- string loldns = serverOptions[srv];
- IPAddress ipa = Dns.GetHostAddresses(loldns).First();
- PingReply rpl = ping.Send(ipa, 1000, buffer, opt);
- switch (rpl.Status)
- {
- case IPStatus.Success:
- if (rpl.RoundtripTime > Properties.Settings.Default.AlertWhen)
- showBalloon("Your ping to the " + srv + "-server is " + rpl.RoundtripTime + "ms");
- break;
- case IPStatus.TimedOut: showBalloon("Connection to the " + srv + "-server has timed out - You should NOT start a game now"); break;
- default: showBalloon("Ping failed: " + rpl.Status.ToString()); break;
- }
- }
- private void showBalloon(string txt)
- {
- tray.ShowBalloonTip(2000, "LoLPing", txt, ToolTipIcon.None);
- }
- private void setAlert(object sender, EventArgs e)
- {
- MenuItem mi = (MenuItem)sender;
- foreach (MenuItem tmpmi in alerts) tmpmi.Checked = false;
- mi.Checked = true;
- Properties.Settings.Default.AlertWhen = int.Parse(mi.Text);
- }
- private void setServer(object sender, EventArgs e)
- {
- MenuItem mi = (MenuItem)sender;
- foreach (MenuItem tmpmi in servers) tmpmi.Checked = false;
- mi.Checked = true;
- Properties.Settings.Default.Server = mi.Text;
- pingNow();
- }
- private void startWithWindows(object sender, EventArgs e)
- {
- runWithWindows = (runWithWindows) ? false : true;
- if (runWithWindows) rk.SetValue("LoLPing", "\"" + Application.ExecutablePath.ToString() + "\""); else rk.DeleteValue("LoLPing");
- menu.MenuItems[1].Checked = runWithWindows;
- }
- private void Exit(object sender, EventArgs e)
- {
- Application.Exit();
- }
- protected override void Dispose(bool isDisposing)
- {
- if (isDisposing) tray.Dispose();
- base.Dispose(isDisposing);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement