aacable79

c# pppoe dialer updated with avg speed display

Mar 24th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 35.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using DotRas;
  11. using Microsoft.Win32;
  12. using System.Net;
  13. using System.IO;
  14. using System.Net.NetworkInformation;
  15. using System.Net.Sockets;
  16. using System.Diagnostics;
  17.  
  18.  
  19. namespace pppoe_dialer___zaib_last
  20. {
  21.  
  22.  
  23.     public partial class Form1 : Form
  24.     {
  25.         public class Globals
  26.         {
  27.             public static string PPPNAME = "Internet-Dialer";
  28.         }
  29.         private Timer _timer;
  30.         private DateTime _startTime = DateTime.MinValue;
  31.         private TimeSpan _currentElapsedTime = TimeSpan.Zero;
  32.         private TimeSpan _totalElapsedTime = TimeSpan.Zero;
  33.         private bool _timerRunning = false;
  34.  
  35.         DateTime _started = DateTime.UtcNow;
  36.         DateTime startTime = new DateTime();
  37.         private NetworkInterface[] nicArr;
  38.         int lanInterval = 2; //3 sec
  39.         int netInterval = 10; //10 sec
  40.         private Timer timer;
  41.         private const double timerUpdate = 1000;
  42.         IPv4InterfaceStatistics interfaceStats;
  43.         private long _lastBytesRecevied;
  44.         private long _lastBytesSent;
  45.         private DateTime _lastReceivedMesurement;
  46.         private DateTime _lastSentMesurement;
  47.  
  48.         //private bool willClose;
  49.         private bool connected;
  50.         private RasHandle handle = null;
  51.         private RasHandle Rashandler = null;
  52.         private RasConnection connection = null;
  53.         //DateTime startTime = new DateTime();
  54.  
  55.         public Form1()
  56.         {
  57.             InitializeComponent();
  58.             // Set up a timer and fire the Tick event once per second (1000 ms)
  59.             //textBox2.PasswordChar='*';
  60.             InitializeNetworkInterface();
  61.             InitializeTimer();
  62.             _timer = new Timer();
  63.             _timer.Interval = 1000;
  64.             _timer.Tick += new EventHandler(_timer_Tick);
  65.  
  66.  
  67.             Timer t = new Timer();
  68.             t.Interval = 1000; //1 sec
  69.             t.Tick += new EventHandler(t_Tick);
  70.             t.Start();
  71.  
  72.             var conns = RasConnection.GetActiveConnections();
  73.             var conn = conns.FirstOrDefault(o => o.EntryName == Globals.PPPNAME);
  74.             if (conn != null)
  75.             {
  76.                 startelptimer();
  77.             }
  78.             if (conn != null)
  79.             {
  80.                 var local = NetworkInterface.GetAllNetworkInterfaces().Where(i => i.Name == Globals.PPPNAME).FirstOrDefault();
  81.                 var stringAddress = local.GetIPProperties().UnicastAddresses[0].Address.ToString();
  82.                 var ipAddress1 = IPAddress.Parse(stringAddress);
  83.                 pppip.Text = "Your Internet IP is : " + ipAddress1;
  84.                 notifyIcon1.Icon = Resource1.icon_online;
  85.                 this.pictureBox2.Image = Resource1.net_conn_image_1;
  86.  
  87.             }
  88.             else
  89.             {
  90.                 this.pictureBox2.Image = Resource1.net_disconnected_image_2;
  91.                 sl2.Text = "Not Connected";
  92.                 //this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "Dialer already connected!"));
  93.             }
  94.  
  95.         }
  96.  
  97.         void _timer_Tick(object sender, EventArgs e)
  98.         {
  99.             // We do this to chop off any stray milliseconds resulting from
  100.             // the Timer's inherent inaccuracy, with the bonus that the
  101.             // TimeSpan.ToString() method will now show correct HH:MM:SS format
  102.             var timeSinceStartTime = DateTime.Now - _startTime;
  103.             timeSinceStartTime = new TimeSpan(timeSinceStartTime.Hours,
  104.                                               timeSinceStartTime.Minutes,
  105.                                               timeSinceStartTime.Seconds);
  106.  
  107.             // The current elapsed time is the time since the start button was
  108.             // clicked, plus the total time elapsed since the last reset
  109.             _currentElapsedTime = timeSinceStartTime + _totalElapsedTime;
  110.  
  111.             // These are just two Label controls which display the current
  112.             // elapsed time and total elapsed time
  113.             //sl.Text = _currentElapsedTime.ToString();
  114.             sl2.Text = timeSinceStartTime.ToString();
  115.         }
  116.  
  117.         private void timerdialer_Tick(object sender, EventArgs e)
  118.         {
  119.             var timeSinceStartTime = DateTime.Now - _startTime;
  120.             timeSinceStartTime = new TimeSpan(timeSinceStartTime.Hours,
  121.                                               timeSinceStartTime.Minutes,
  122.                                               timeSinceStartTime.Seconds);
  123.  
  124.             // The current elapsed time is the time since the start button was
  125.             // clicked, plus the total time elapsed since the last reset
  126.             _currentElapsedTime = timeSinceStartTime + _totalElapsedTime;
  127.  
  128.             // These are just two Label controls which display the current
  129.             // elapsed time and total elapsed time
  130.             //sl.Text = _currentElapsedTime.ToString();
  131.             sl2.Text = timeSinceStartTime.ToString();
  132.         }
  133.  
  134.  
  135.         private void timerStart_Click(object sender, EventArgs e)
  136.         {
  137.             // If the timer isn't already running
  138.             // Set the start time to Now
  139.             _startTime = DateTime.Now;
  140.  
  141.             // Store the total elapsed time so far
  142.             //_totalElapsedTime = _currentElapsedTime;
  143.  
  144.             _timer.Start();
  145.             _timerRunning = true;
  146.  
  147.         }
  148.  
  149.         private void InitializeNetworkInterface()
  150.         {
  151.  
  152.             // Grab all local interfaces to this computer
  153.             nicArr = NetworkInterface.GetAllNetworkInterfaces();
  154.             // Add each interface name to the combo box
  155.             for (int i = 0; i < nicArr.Length; i++)
  156.                 cmbInterface.Items.Add(nicArr[i].Name);
  157.             // Change the initial selection to the first interface
  158.             cmbInterface.SelectedIndex = 0;
  159.         }
  160.  
  161.         private void InitializeTimer()
  162.         {
  163.             timer = new Timer();
  164.             timer.Interval = (int)timerUpdate;
  165.             timer.Tick += new EventHandler(timer_Tick);
  166.             timer.Start();
  167.         }
  168.  
  169.         void t_Tick(object sender, EventArgs e)
  170.         {
  171.             lanInterval--;
  172.             netInterval--;
  173.  
  174.             if (lanInterval == 0)
  175.             {
  176.                 checkpppstatus();
  177.                 lanInterval = 2; //reset to base value
  178.             }
  179.  
  180.             if (netInterval == 0)
  181.             {
  182.                 checknetstatus();
  183.                 netInterval = 10; //reset to base value
  184.             }
  185.         }
  186.         protected void checkpppstatus()
  187.         {
  188.             this.Invoke((MethodInvoker)delegate
  189.             {
  190.                 var conns = RasConnection.GetActiveConnections();
  191.                 var conn = conns.FirstOrDefault(o => o.EntryName == Globals.PPPNAME);
  192.                 if (conn != null)
  193.                 {
  194.                     connstatus.Text = "Dialer Status: Connected!";
  195.                     connstatus.ForeColor = System.Drawing.Color.Green;
  196.                 }
  197.                 else
  198.                 {
  199.                     connstatus.Text = "Dialer Status: Disconnected!";
  200.                     connstatus.ForeColor = System.Drawing.Color.Red;
  201.                     pppip.Text = "Your Internet IP is : Not Connected!";
  202.                     sl2.Text = "Not Connected";
  203.                 }
  204.             });
  205.         }
  206.  
  207.  
  208.         protected void checknetstatus()
  209.         {
  210.             if (IsOnline(textBox1.Text) == true)
  211.             {
  212.                 internetStatus.ForeColor = System.Drawing.Color.Green;
  213.                 string Status = "Internet Stauts: UP";
  214.                 internetStatus.Text = Status;
  215.             }
  216.             else
  217.             {
  218.                 internetStatus.ForeColor = System.Drawing.Color.Red;
  219.                 string Status = "Internet Stauts: DOWN";
  220.                 internetStatus.Text = Status;
  221.             }
  222.         }
  223.  
  224.         protected void Displaynotify()
  225.         {
  226.             try
  227.             {
  228.                 notifyIcon1.BalloonTipTitle = "You have successfully connected to the Internet ...";
  229.                 notifyIcon1.BalloonTipText = "Internet Connected ...";
  230.                 notifyIcon1.Visible = true;
  231.                 notifyIcon1.ShowBalloonTip(5000);
  232.             }
  233.             catch (Exception ex)
  234.             {
  235.             }
  236.         }
  237.  
  238.  
  239.  
  240.         protected void stopelptimer()
  241.         {
  242.             // If the timer isn't already running
  243.             // Set the start time to Now
  244.             _startTime = DateTime.Now;
  245.  
  246.             // Store the total elapsed time so far
  247.  
  248.             //_totalElapsedTime = _currentElapsedTime;
  249.  
  250.             _timer.Stop();
  251.             _timerRunning = false;
  252.             TimeSpan _currentElapsedTime = TimeSpan.Zero;
  253.             TimeSpan _totalElapsedTime = TimeSpan.Zero;
  254.             _totalElapsedTime = _currentElapsedTime;
  255.  
  256.             _timer.Stop();
  257.             _timerRunning = false;
  258.  
  259.         }
  260.  
  261.  
  262.  
  263.  
  264.         protected void startelptimer()
  265.         {
  266.             // If the timer isn't already running
  267.             // Set the start time to Now
  268.             _startTime = DateTime.Now;
  269.  
  270.             // Store the total elapsed time so far
  271.             //_totalElapsedTime = DateTime.Now;
  272.             _timer.Start();
  273.             _timerRunning = true;
  274.         }
  275.  
  276.         protected void Displaynotifyfordisconnect()
  277.         {
  278.             try
  279.             {
  280.                 notifyIcon1.BalloonTipTitle = "My PPPoE Dialer DISCONNECTED !!!";
  281.                 notifyIcon1.BalloonTipText = "Internet Disconnected !!!";
  282.                 notifyIcon1.Visible = true;
  283.                 notifyIcon1.ShowBalloonTip(5000);
  284.                 //                MessageBox.Show("DC");
  285.                 {
  286.                     this.Invoke((MethodInvoker)delegate
  287.                     {
  288.                         this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "\r\nDialer Disconnected!"));
  289.                     });
  290.                 }
  291.             }
  292.             catch (Exception ex)
  293.             {
  294.             }
  295.         }
  296.  
  297.  
  298.         private void saveCredential(string username, string password, bool remember, bool auto)
  299.         {
  300.             RegistryKey hkcu = Registry.CurrentUser;
  301.             RegistryKey software = hkcu.OpenSubKey("Software", true);
  302.             RegistryKey zaib = software.CreateSubKey("zaib");
  303.             zaib.SetValue("username", username, RegistryValueKind.String);
  304.             zaib.SetValue("password", password, RegistryValueKind.String);
  305.             zaib.Close();
  306.         }
  307.  
  308.  
  309.         private void readCredential()
  310.         {
  311.             RegistryKey hkcu = Registry.CurrentUser;
  312.             RegistryKey software = hkcu.OpenSubKey("Software", true);
  313.             RegistryKey zaib = software.CreateSubKey("zaib");
  314.             try
  315.             {
  316.                 textBox1.Text = (string)zaib.GetValue("username", "");
  317.                 textBox2.Text = (string)zaib.GetValue("password", "");
  318.                 //checkRemember.Checked = ((int)zaib.GetValue("remember") == 1);
  319.                 //checkAuto.Checked = ((int)zaib.GetValue("auto") == 1);
  320.             }
  321.             catch (Exception ex)
  322.             {
  323.                 // never saved
  324.             }
  325.             zaib.Close();
  326.         }
  327.  
  328.  
  329.         private void Form1_Move_1(object sender, EventArgs e)
  330.         {
  331.             if (this.WindowState == FormWindowState.Minimized)
  332.             {
  333.                 this.Hide();
  334.                 notifyIcon1.ShowBalloonTip(2000, "My PPPoE Dialer", "The App has be moved to the tray.", ToolTipIcon.Info);
  335.                 checkpppstatus();
  336.                 //timer.Dispose();
  337.                 //this.Dispose();
  338.             }
  339.         }
  340.  
  341.  
  342.         private void button1_Click(object sender, EventArgs e)
  343.         {
  344.             string path;
  345.             path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
  346.             using (RasPhoneBook pbk = new RasPhoneBook())
  347.             {
  348.                 pbk.Open(path);
  349.                 RasEntry entry = RasEntry.CreateBroadbandEntry(Globals.PPPNAME, RasDevice.GetDeviceByName("PPPOE", RasDeviceType.PPPoE, false));
  350.                 // Configure any options for your entry here via entry.Options
  351.                 entry.RedialCount = 99;
  352.                 // Finally Add the PPPOE Dialer in the network connection , hurrahhhh , zaib
  353.                 // If Preiovus Entry found, delete, and reacreate
  354.                 var conns = RasConnection.GetActiveConnections();
  355.                 var conn = conns.FirstOrDefault(o => o.EntryName == Globals.PPPNAME);
  356.                 if (conn != null)
  357.                 {
  358.                     MessageBox.Show("Same Dialer Already Connected! First Disconnect it to re-create new.");
  359.                     return;
  360.                 }
  361.                 pbk.Entries.Clear();
  362.                 rasDialer1.Credentials = new System.Net.NetworkCredential(textBox1.Text, textBox2.Text);
  363.                 rasDialer1.AllowUseStoredCredentials = true;
  364.  
  365.                 pbk.Entries.Add(entry);
  366.                 // If Dialer Create, show successfull message.
  367.                 MessageBox.Show("Internet Dialer created Successfully.");
  368.                 saveCredential("test", "test", true, true);
  369.                 ///// SHORTCUT BEGINE
  370.  
  371.                 string destDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  372.                 string destFileName = @"Connect To My Internet Dialer.lnk";
  373.  
  374.                 // Create .lnk file
  375.                 string path2 = System.IO.Path.Combine(destDir, destFileName);
  376.                 FileStream fs = File.Create(path2);
  377.                 fs.Close();
  378.  
  379.                 // Instantiate a ShellLinkObject that references the .lnk we created
  380.                 Shell32.Shell shell = new Shell32.Shell();
  381.                 Shell32.Folder shellFolder = shell.NameSpace(destDir);
  382.                 Shell32.FolderItem shellFolderItem = shellFolder.Items().Item(destFileName);
  383.                 Shell32.ShellLinkObject shellLinkObject = (Shell32.ShellLinkObject)shellFolderItem.GetLink;
  384.  
  385.                 // Set .lnk properties
  386.                 shellLinkObject.Arguments = "-d pppoe2";
  387.                 shellLinkObject.Description = Globals.PPPNAME;
  388.                 shellLinkObject.Path = @"%windir%\System32\rasphone.exe";
  389.                 shellLinkObject.WorkingDirectory = "%windir%";
  390.  
  391.                 shellLinkObject.Save(path2);
  392.                 //// SHORTCUT END
  393.  
  394.  
  395.  
  396.             }
  397.         }
  398.  
  399.         private void Form1_Load(object sender, EventArgs e)
  400.         {
  401.  
  402.         }
  403.         private void textBox1_TextChanged(object sender, EventArgs e)
  404.         {
  405.         }
  406.         private void textBox2_TextChanged(object sender, EventArgs e)
  407.         {
  408.         }
  409.         private void textBox1_Click(object sender, EventArgs e)
  410.         {
  411.             //    textBox1.Clear();
  412.         }
  413.         private void Button2_Click(object sender, EventArgs e)
  414.         {
  415.             this.Close();
  416.         }
  417.         private void textBox2_Click(object sender, EventArgs e)
  418.         {
  419.             //textBox2.Clear();
  420.         }
  421.         private void label1_Click_1(object sender, EventArgs e)
  422.         {
  423.             System.Diagnostics.Process.Start("https://aacable.wordpress.com");
  424.         }
  425.         private void pictureBox2_Click(object sender, EventArgs e)
  426.         {
  427.             System.Diagnostics.Process.Start("https://aacable.wordpress.com");
  428.         }
  429.         private void button2_Click_1(object sender, EventArgs e)
  430.         {
  431.  
  432.             RasHandle handle = null;
  433.             using (RasDialer dialer = new RasDialer())
  434.             {
  435.                 dialer.StateChanged += new EventHandler<StateChangedEventArgs>(rasDialer1_StateChanged);
  436.                 dialer.EntryName = (Globals.PPPNAME);
  437.                 {
  438.                     // this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "OK ...", "{0}\r\n\r\n"));
  439.                 };
  440.  
  441.                 //this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "Connection in progress ...", "{0}\r\n\r\n"));
  442.                 dialer.StateChanged += new EventHandler<StateChangedEventArgs>(rasDialer1_StateChanged);
  443.                 dialer.EntryName = (Globals.PPPNAME);
  444.                 string username = textBox1.Text;
  445.                 string passwd = textBox2.Text;
  446.                 dialer.Credentials = new System.Net.NetworkCredential(textBox1.Text, textBox2.Text);
  447.                 dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
  448.                 dialer.Timeout = 1000;
  449.                 dialer.AllowUseStoredCredentials = true;
  450.                 dialer.EntryName = (Globals.PPPNAME);
  451.                 rasDialer1.EntryName = (Globals.PPPNAME);
  452.                 rasDialer1.Credentials = new System.Net.NetworkCredential(textBox1.Text, textBox2.Text);
  453.                 rasDialer1.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
  454.  
  455.                 // If username or password window is empty , post error
  456.                 if (string.IsNullOrWhiteSpace(textBox1.Text))
  457.                 {
  458.                     this.StatusTextBox.AppendText(string.Format("{0}\r\n\r", "You must enter username/password in order to dial", "{0}\r\n"));
  459.                     //MessageBox.Show("Enter username.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  460.                     return;
  461.                 }
  462.                 //
  463.                 var conns = RasConnection.GetActiveConnections();
  464.                 var conn = conns.FirstOrDefault(o => o.EntryName == Globals.PPPNAME);
  465.                 if (conn != null)
  466.                 {
  467.                     this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "Dialer already connected!"));
  468.                     //MessageBox.Show("Dialer - Already connected!");
  469.                 }
  470.                 else
  471.  
  472.                     handle = rasDialer1.DialAsync();
  473.             }
  474.         }
  475.  
  476.         private void StatusTextBox_TextChanged(object sender, EventArgs e)
  477.         {
  478.             //   MessageBox.Show("abc1");
  479.         }
  480.  
  481.         private void rasDialer1_StateChanged(object sender, StateChangedEventArgs e)
  482.         {
  483.             this.Invoke((MethodInvoker)delegate
  484.             {
  485.                 this.StatusTextBox.AppendText(string.Format(e.State.ToString() + "\r\n"));
  486.                 checkpppstatus();
  487.             });
  488.         }
  489.  
  490.  
  491.         private void rasDialer1_DialCompleted(object sender, DialCompletedEventArgs e)
  492.         {
  493.             {
  494.                 if (e.Cancelled)
  495.                 {
  496.                     MessageBox.Show("Cancelled");
  497.                 }
  498.                 else if (e.TimedOut)
  499.                 {
  500.                     MessageBox.Show("Time out");
  501.                 }
  502.                 else if (e.Error != null)
  503.                 {
  504.                     MessageBox.Show(e.Error.ToString(), "Error");
  505.                 }
  506.                 else if (e.Connected)
  507.                 {
  508.                     //label9.Text = 0 + "";
  509.                     //timerdialer.Start();
  510.  
  511.                     //MessageBox.Show("Connection successful zaib!");
  512.                     //this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "Connected!"));
  513.                     //string newText = "abc";
  514.                     var conn = RasConnection.GetActiveConnections().Where(c => c.EntryName == Globals.PPPNAME).FirstOrDefault();
  515.                     RasIPInfo ipAddresses = (RasIPInfo)conn.GetProjectionInfo(RasProjectionType.IP);
  516.                     this.StatusTextBox.AppendText(string.Format("{0}\r\n", "Your internet ip is", ipAddresses.IPAddress.ToString()));
  517.                     this.StatusTextBox.AppendText(string.Format("{0}  ", ipAddresses.IPAddress.ToString()));
  518.                     saveCredential("test", "test", true, true);
  519.                     var local = NetworkInterface.GetAllNetworkInterfaces().Where(i => i.Name == Globals.PPPNAME).FirstOrDefault();
  520.                     var stringAddress = local.GetIPProperties().UnicastAddresses[0].Address.ToString();
  521.                     var ipAddress1 = IPAddress.Parse(stringAddress);
  522.                     pppip.Text = "Your Internet IP is : " + ipAddress1;
  523.                     //gwLabel.Text = "Gateway is : " + ";
  524.                     ShowInTaskbar = true;
  525.                     this.Hide();
  526.                     InitializeNetworkInterface();
  527.                     //Displaynotify();
  528.                     UpdateNetworkInterface();
  529.                     stopelptimer();
  530.                     startelptimer();
  531.                 }
  532.             }
  533.         }
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.         private void dcButton_Click(object sender, EventArgs e)
  541.         {
  542.             var conns = RasConnection.GetActiveConnections();
  543.             var conn = conns.FirstOrDefault(o => o.EntryName == Globals.PPPNAME);
  544.             if (conn != null)
  545.             {
  546.                 //InitializeNetworkInterface();
  547.                 conn.HangUp();
  548.  
  549.                 //MessageBox.Show("Disconnect on User Request");
  550.                 this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "\r\nDisconnected on user request !"));
  551.                 //timer.Dispose();
  552.  
  553.                 //lblSpeed.Text = "-";
  554.                 lblInterfaceType.Text = "-";
  555.                 //lblSpeed.Text = "-";
  556.                 //lblBytesReceived.Text = "-";
  557.                 //lblBytesSent.Text = "-";
  558.                 lblUpload.Text = "-";
  559.                 lblDownload.Text = "-";
  560.  
  561.                 connstatus.ForeColor = Color.Red;
  562.                 checkpppstatus();
  563.                 this.Hide();
  564.                 Displaynotifyfordisconnect();
  565.             }
  566.             else
  567.                 MessageBox.Show("Dialer is not Active !", Globals.PPPNAME);
  568.         }
  569.  
  570.  
  571.  
  572.  
  573.         private void rasDialer1_Error(object sender, System.IO.ErrorEventArgs e)
  574.         {
  575.             //this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "\r\n AUTO DC DONT KNOW !"));
  576.             MessageBox.Show("UNKOWN!!!!!!!!!!!!");
  577.         }
  578.  
  579.  
  580.  
  581.         private void Form1_Resize(object sender, EventArgs e)
  582.         {
  583.             notifyIcon1.Text = "My Internet Dialer";
  584.         }
  585.  
  586.         private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  587.         {
  588.             this.Show();
  589.             this.WindowState = FormWindowState.Normal;
  590.         }
  591.  
  592.  
  593.         private void notifyIcon1_MouseDoubleClick_1(object sender, MouseEventArgs e)
  594.         {
  595.             Show();
  596.             WindowState = FormWindowState.Normal;
  597.         }
  598.  
  599.         private void notifyIcon1_MouseMove(object sender, MouseEventArgs e)
  600.         {
  601.             notifyIcon1.Text = "My Internet Dialer";
  602.         }
  603.  
  604.         private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
  605.         {
  606.  
  607.         }
  608.  
  609.  
  610.  
  611.  
  612.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  613.         {
  614.  
  615.  
  616.         }
  617.  
  618.         private void notifyIcon1_BalloonTipShown(object sender, EventArgs e)
  619.         {
  620.         }
  621.  
  622.         private void Form1_Load_1(object sender, EventArgs e)
  623.         {
  624.             timer4dt.Start();
  625.             datelabel.Text = DateTime.Now.ToLongDateString();
  626.             timelabel.Text = DateTime.Now.ToLongTimeString();
  627.             readCredential();
  628.             checkpppstatus();
  629.             Timer MyTimer = new Timer();
  630.             MyTimer.Interval = 5000;
  631.             MyTimer.Tick += new EventHandler(MyTimer_Tick);
  632.             MyTimer.Start();
  633.  
  634.             Timer MyTimer10 = new Timer();
  635.             MyTimer10.Interval = 5000;
  636.             MyTimer10.Tick += new EventHandler(MyTimer_Tick);
  637.             MyTimer10.Start();
  638.             var conns = RasConnection.GetActiveConnections();
  639.             var conn = conns.FirstOrDefault(o => o.EntryName == Globals.PPPNAME);
  640.             if (conn != null)
  641.                 this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "Dialer already connected!"));
  642.         }
  643.         private void MyTimer_Tick(object sender, EventArgs e)
  644.         {
  645.             //checkpppstatus();
  646.         }
  647.         private void MyTimer10_Tick(object sender, EventArgs e)
  648.         {
  649.             //    checknetstatus();
  650.         }
  651.  
  652.  
  653.         private void contextMenuStrip1_DoubleClick(object sender, EventArgs e)
  654.         {
  655.             this.Show();
  656.         }
  657.  
  658.         private void showToolStripMenuItem_Click(object sender, EventArgs e)
  659.         {
  660.             this.Show();
  661.         }
  662.  
  663.         private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  664.         {
  665.             Application.Exit();
  666.         }
  667.  
  668.         private void notifyIcon1_Click(object sender, EventArgs e)
  669.         {
  670.             this.Show();
  671.             WindowState = FormWindowState.Normal;
  672.         }
  673.  
  674.         private void label3_Click(object sender, EventArgs e)
  675.         {
  676.  
  677.         }
  678.  
  679.         private void connstatus_Click(object sender, EventArgs e)
  680.         {
  681.  
  682.         }
  683.  
  684.         private void connstatus_Click_1(object sender, EventArgs e)
  685.         {
  686.  
  687.         }
  688.  
  689.  
  690.         private void rasDialDialog1_Error(object sender, RasErrorEventArgs e)
  691.         {
  692.         }
  693.  
  694.         private void rasConnectionWatcher1_Disconnected(object sender, RasConnectionEventArgs e)
  695.         {
  696.             checkpppstatus();
  697.             notifyIcon1.Icon = Resource1.icon_standby;
  698.             this.pictureBox2.Image = Resource1.net_disconnected_image_2;
  699.             Displaynotifyfordisconnect();
  700.             //i = +0;
  701.             stopelptimer();
  702.  
  703.  
  704.         }
  705.  
  706.         private void rasConnectionWatcher1_Connected(object sender, RasConnectionEventArgs e)
  707.         {
  708.             UpdateNetworkInterface();
  709.             Displaynotify();
  710.             //startelptimer();
  711.             notifyIcon1.Icon = Resource1.icon_online;
  712.             this.pictureBox2.Image = Resource1.net_conn_image_1;
  713.         }
  714.  
  715.         private void rasConnectionWatcher1_Error(object sender, ErrorEventArgs e)
  716.         {
  717.             MessageBox.Show("ERR");
  718.         }
  719.  
  720.         private void rasPhoneBookDialog1_ChangedEntry(object sender, RasPhoneBookDialogEventArgs e)
  721.         {
  722.             MessageBox.Show("ENTRY CHANGED");
  723.         }
  724.  
  725.  
  726.         // Tafreeh start
  727.  
  728.         public bool IsOnline(string website)
  729.         {
  730.             Ping x = new Ping();
  731.             PingReply reply = x.Send(IPAddress.Parse("8.8.8.8")); //enter ip of the machine
  732.             if (reply.Status == IPStatus.Success) // here we check for the reply status if it is success it means the host is reachable
  733.             {
  734.                 return true;
  735.                 //     status.Text = "Available. And Round Trip Time of the packet is:" + reply.RoundtripTime.ToString();
  736.             }
  737.             else //if host is not reachable.
  738.                 return false;
  739.             //   status.Text = "Not available";
  740.         }
  741.         //}
  742.  
  743.  
  744.  
  745.         private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  746.         {
  747.             System.Diagnostics.Process.Start("https://aacable.wordpress.com");
  748.         }
  749.  
  750.         // // INTERFACE DOWNLOAD UPLOAD SETTINGA
  751.         private void UpdateNetworkInterface()
  752.         {
  753.             this.Invoke((MethodInvoker)delegate
  754.             {
  755.                 NetworkInterface nic = nicArr[cmbInterface.SelectedIndex];
  756.                 interfaceStats = nic.GetIPv4Statistics();
  757.                 long lngBytesSent = 0;
  758.                 long lngBtyesReceived = 0;
  759.                 int bytesSentSpeed = (int)(interfaceStats.BytesSent - lngBytesSent) / 1024;
  760.                 int bytesReceivedSpeed = (int)(interfaceStats.BytesReceived - lngBtyesReceived) / 1024;
  761.  
  762.                 _lastReceivedMesurement = DateTime.UtcNow;
  763.                 _lastBytesRecevied = interfaceStats.BytesReceived;
  764.  
  765.                 _lastSentMesurement = DateTime.UtcNow;
  766.                 _lastBytesSent = interfaceStats.BytesSent;
  767.  
  768.                 var averagedownspeed = getKBDownloadSpeed();
  769.                 var averageupspeed = getKBUploadSpeed();
  770.  
  771.                 // Update the labels
  772.                 lblInterfaceType.Text = nic.NetworkInterfaceType.ToString();
  773.                 lblUpload.Text = bytesSentSpeed.ToString() + " KB";
  774.                 lblDownload.Text = bytesReceivedSpeed.ToString() + " KB";
  775.                 this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", averagedownspeed));
  776.                 this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", averageupspeed));
  777.             });
  778.         }
  779.  
  780.         private double getKBDownloadSpeed()
  781.         {
  782.             double result = (interfaceStats.BytesReceived - _lastBytesRecevied) / (DateTime.UtcNow - _lastReceivedMesurement).TotalSeconds;
  783.  
  784.             _lastReceivedMesurement = DateTime.UtcNow;
  785.             _lastBytesRecevied = interfaceStats.BytesReceived;
  786.  
  787.             return result / 1024d;
  788.         }
  789.  
  790.         private double getKBUploadSpeed()
  791.         {
  792.             double result = (interfaceStats.BytesSent - _lastBytesSent) / (DateTime.UtcNow - _lastSentMesurement).TotalSeconds;
  793.  
  794.             _lastSentMesurement = DateTime.UtcNow;
  795.             _lastBytesSent = interfaceStats.BytesSent;
  796.  
  797.             return result / 1024d;
  798.         }
  799.  
  800.  
  801.         void timer_Tick(object sender, EventArgs e)
  802.         {
  803.             //InitializeNetworkInterface();
  804.             var conns = RasConnection.GetActiveConnections();
  805.             var conn = conns.FirstOrDefault(o => o.EntryName == Globals.PPPNAME);
  806.             if (conn != null)
  807.                 UpdateNetworkInterface();
  808.         }
  809.  
  810.         private void cmbInterface_SelectedIndexChanged(object sender, EventArgs e)
  811.         {
  812.  
  813.         }
  814.  
  815.  
  816.         private void dialerCheck_Click(object sender, EventArgs e)
  817.         {
  818.             //Gateway IP
  819.             NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
  820.             foreach (NetworkInterface adapter in adapters)
  821.             {
  822.                 IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
  823.                 GatewayIPAddressInformationCollection addresses =
  824.                                            adapterProperties.GatewayAddresses;
  825.                 if (addresses.Count > 0)
  826.                 {
  827.                     foreach (GatewayIPAddressInformation address in addresses)
  828.                     {
  829.                         StatusTextBox.Text += "Gateway Address  | " +
  830.                                             address.Address.ToString() + "\r\n";
  831.  
  832.                     }
  833.                 }
  834.             }
  835.         }
  836.  
  837.  
  838.  
  839.  
  840.  
  841.         private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  842.         {
  843.             //            timer.Dispose();
  844.             //            this.Dispose();
  845.         }
  846.  
  847.         private void timer4dt_Tick(object sender, EventArgs e)
  848.         {
  849.             timelabel.Text = DateTime.Now.ToLongTimeString();
  850.             timer4dt.Start();
  851.         }
  852.  
  853.  
  854.  
  855.  
  856.  
  857.         private void resetStats_Click(object sender, EventArgs e)
  858.         {
  859.  
  860.             _timer.Stop();
  861.             _timerRunning = false;
  862.  
  863.             // Reset the elapsed time TimeSpan objects
  864.             _totalElapsedTime = TimeSpan.Zero;
  865.             _currentElapsedTime = TimeSpan.Zero;
  866.  
  867.  
  868.             //label9.Text = "--";
  869.             //label9.Text =0+"";
  870.         }
  871.  
  872.  
  873.  
  874.  
  875.         private void timerStart_Click_1(object sender, EventArgs e)
  876.         {
  877.             // If the timer isn't already running
  878.             if (!_timerRunning)
  879.             {
  880.                 // Set the start time to Now
  881.                 _startTime = DateTime.Now;
  882.  
  883.                 // Store the total elapsed time so far
  884.  
  885.                 _totalElapsedTime = _currentElapsedTime;
  886.  
  887.                 _timer.Start();
  888.                 _timerRunning = true;
  889.             }
  890.             else // If the timer is already running
  891.             {
  892.                 _startTime = DateTime.Now;
  893.  
  894.                 // Store the total elapsed time so far
  895.  
  896.                 _totalElapsedTime = _currentElapsedTime;
  897.  
  898.                 _timer.Start();
  899.                 _timerRunning = true;
  900.             }
  901.         }
  902.  
  903.  
  904.         private void timerdialer_Tick_1(object sender, EventArgs e)
  905.         {
  906.             {
  907.                 // We do this to chop off any stray milliseconds resulting from
  908.                 // the Timer's inherent inaccuracy, with the bonus that the
  909.                 // TimeSpan.ToString() method will now show correct HH:MM:SS format
  910.                 var timeSinceStartTime = DateTime.Now - _startTime;
  911.                 timeSinceStartTime = new TimeSpan(timeSinceStartTime.Hours,
  912.                                                   timeSinceStartTime.Minutes,
  913.                                                   timeSinceStartTime.Seconds);
  914.  
  915.                 // The current elapsed time is the time since the start button was
  916.                 // clicked, plus the total time elapsed since the last reset
  917.                 _currentElapsedTime = timeSinceStartTime + _totalElapsedTime;
  918.             }
  919.  
  920.  
  921.  
  922.             //TimeSpan t = TimeSpan.FromSeconds(seconds);
  923.             //this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "\r\nDialer Disconnected!"));
  924.         }
  925.  
  926.         private void timerStop_Click(object sender, EventArgs e)
  927.         {
  928.             _timer.Stop();
  929.             _timerRunning = false;
  930.         }
  931.  
  932.         // tafreh2 start
  933.         protected void checknetstatusBoxMsg()
  934.         {
  935.             if (IsOnline(textBox1.Text) == true)
  936.             {
  937.                 internetStatus.ForeColor = System.Drawing.Color.Green;
  938.                 string Status = "Internet Stauts: Connected by testing 8.8.8.8";
  939.                 internetStatus.Text = Status;
  940.                 this.StatusTextBox.AppendText(string.Format("{0}\r\n", Status));
  941.             }
  942.             else
  943.             {
  944.                 internetStatus.ForeColor = System.Drawing.Color.Red;
  945.                 string Status = "Internet Stauts: DOWN by testing 8.8.8.8";
  946.                 this.StatusTextBox.AppendText(string.Format("{0}\r\n ", Status, "{0}\r\n\r\n"));
  947.             }
  948.         }
  949.  
  950.  
  951.         protected void checkpppstatusBoxMsg()
  952.         {
  953.             this.Invoke((MethodInvoker)delegate
  954.             {
  955.                 var conns = RasConnection.GetActiveConnections();
  956.                 var conn = conns.FirstOrDefault(o => o.EntryName == Globals.PPPNAME);
  957.                 if (conn != null)
  958.                 {
  959.                     string Status = "PPP Dialer Stauts: Connected";
  960.                     this.StatusTextBox.AppendText(string.Format("{0}\r\n", Status));
  961.                 }
  962.                 else
  963.                 {
  964.                     string Status = "PPP Dialer Stauts: Disconnected";
  965.                     this.StatusTextBox.AppendText(string.Format("{0}\r\n", Status));
  966.                 }
  967.  
  968.             });
  969.         }
  970.  
  971.  
  972.  
  973.  
  974.         // tafreh2 end
  975.  
  976.         private void netStatusButton_Click(object sender, EventArgs e)
  977.         {
  978.             checknetstatusBoxMsg();
  979.         }
  980.  
  981.         private void dialerLanStatus_Click(object sender, EventArgs e)
  982.         {
  983.             checkpppstatusBoxMsg();
  984.         }
  985.  
  986.  
  987.         private void gatewayDetails_Click(object sender, EventArgs e)
  988.         {
  989.             //Gateway IP
  990.             NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
  991.             this.StatusTextBox.AppendText(string.Format("{0}\r\n", "Gateway Details!"));
  992.             foreach (NetworkInterface adapter in adapters)
  993.             {
  994.                 IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
  995.                 GatewayIPAddressInformationCollection addresses =
  996.                                            adapterProperties.GatewayAddresses;
  997.                 if (addresses.Count > 0)
  998.                 {
  999.                     foreach (GatewayIPAddressInformation address in addresses)
  1000.                     {
  1001.                         this.StatusTextBox.AppendText(string.Format(address.Address.ToString() + "\r\n"));
  1002.                     }
  1003.                 }
  1004.             }
  1005.         }
  1006.  
  1007.  
  1008.         private void StatusTextBox_VisibleChanged(object sender, EventArgs e)
  1009.         {
  1010.             StatusTextBox.SelectionStart = textBox1.Text.Length;
  1011.             StatusTextBox.ScrollToCaret();
  1012.         }
  1013.  
  1014.         private void label5_Click(object sender, EventArgs e)
  1015.         {
  1016.  
  1017.         }
  1018.  
  1019.  
  1020.  
  1021.     }
  1022. }
Add Comment
Please, Sign In to add comment