/MyIPForm.cs
C# | 169 lines | 139 code | 20 blank | 10 comment | 25 complexity | 3e2635d80f89a2963e8dff62f47b3be0 MD5 | raw file
- using System;
- using System.Net.NetworkInformation;
- using System.Reflection;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- using MyIP.Localization;
-
- namespace MyIP
- {
- public partial class MyIP : Form
- {
- public MyIP()
- {
- //Thread.CurrentThread.CurrentUICulture = new CultureInfo("sv-SE");
-
- InitializeComponent();
-
- StartPosition = FormStartPosition.CenterScreen;
-
- Version version = Assembly.GetExecutingAssembly().GetName().Version;
- int major = version.Major;
- int minor = version.Minor;
- int build = version.Build;
-
- Text = String.Format("{0} {1}.{2}", Language.MyIPForm.Title, major, minor);
- btnAbout.Text = Language.MyIPForm.btnAbout;
- btnRefresh.Text = Language.MyIPForm.btnRefresh;
- btnExit.Text = Language.MyIPForm.btnExit;
- boxExternalIP.Text = Language.MyIPForm.boxExternalIP;
- boxInternalIP.Text = Language.MyIPForm.boxInternalIP;
- btnExtIPCopy.Text = Language.MyIPForm.btnCopy;
- btnIntIPCopy.Text = Language.MyIPForm.btnCopy;
- }
-
- private void RefreshBtn_Click(object sender, EventArgs e)
- {
- string wan = CheckExtIp();
- string lan = CheckIntIp();
- lblExtIP.Text = wan;
- lblIntIP.Text = lan;
-
- // Enable copy buttons first AFTER some content have been entered
- btnIntIPCopy.Enabled = true;
- btnExtIPCopy.Enabled = true;
- }
-
- private void ExitBtn_Click(object sender, EventArgs e)
- {
- this.Close();
- }
-
- private static string CheckExtIp()
- {
- // IPv4 & IPv6: myip.rrnet.info
- // IPv4 only: myip4.rrnet.info
- // IPv6 only: myip6.rrnet.info
-
- // TODO: Error handling if the webserver don't respond or any other error occurs
- string content = Encoding.ASCII.GetString(Http.Get("http://myip4.rrnet.info"));
-
- try
- {
- content = Regex.Replace(content, @"[^\w\.@-]", "");
- }
- catch (Exception ex)
- {
- Program.ShowException(ex);
- }
-
- return content.ToString();
- }
-
- private static bool IsPrivIp(string address)
- {
- bool isPrivate = false;
- if (new IPSubnet("192.168.0.0/16").Contains(address))
- {
- isPrivate = true;
- }
- else if (new IPSubnet("172.16.0.0/12").Contains(address))
- {
- isPrivate = true;
- }
- else if (new IPSubnet("10.0.0.0/8").Contains(address))
- {
- isPrivate = true;
- }
- return isPrivate;
- }
-
- private static string CheckIntIp()
- {
- NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
- if (nics == null || nics.Length < 1)
- {
- MessageBox.Show("No Nics");
- return "";
- }
-
- foreach (NetworkInterface adapter in nics)
- {
- // Ignore Non-IPv4 interfaces
- // Ignore Loopback interfaces
- // Ignore interfaces that are not up
- if ((adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211
- || adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
- && adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback
- && adapter.OperationalStatus == OperationalStatus.Up
- && adapter.Supports(NetworkInterfaceComponent.IPv4) == true)
- {
- foreach (UnicastIPAddressInformation ip in adapter.GetIPProperties().UnicastAddresses)
- {
- if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
- {
- if (IsPrivIp(ip.Address.ToString()))
- {
- return ip.Address.ToString();
- //MessageBox.Show(adapter.Name + "\r\n" + ip.Address.ToString());
- }
- }
- }
- }
- }
- return "";
- }
-
- private void MyIP_Load(object sender, EventArgs e)
- {
-
- }
-
- private void AboutBtn_Click(object sender, EventArgs e)
- {
- Cursor.Current = Cursors.WaitCursor;
- using (AboutForm form = new AboutForm())
- {
- Cursor.Current = Cursors.Default;
- form.ShowDialog();
- }
- }
-
- private void btnExtIPCopy_Click(object sender, EventArgs e)
- {
- Clipboard.SetText(lblExtIP.Text.ToString());
- if (Clipboard.GetText() == lblExtIP.Text.ToString())
- {
- MessageBox.Show(String.Format(Language.MyIPForm.CopySuccess, lblExtIP.Text.ToString()), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- else
- {
- MessageBox.Show(String.Format(Language.MyIPForm.CopyFailed, lblExtIP.Text.ToString()), Language.General.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
-
- private void btIntIPCopy_Click(object sender, EventArgs e)
- {
- Clipboard.SetText(lblIntIP.Text.ToString());
- if (Clipboard.GetText() == lblIntIP.Text.ToString())
- {
- MessageBox.Show(String.Format(Language.MyIPForm.CopySuccess, lblIntIP.Text.ToString()), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- else
- {
- MessageBox.Show(String.Format(Language.MyIPForm.CopyFailed, lblIntIP.Text.ToString()), Language.General.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- }