aacable79

c# pppoe dialer

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