private void buttonCheckHost_Click(object sender, EventArgs e) { var broadcastHost = textBroadcastAddress.Text.Trim(); try { Cursor.Current = Cursors.WaitCursor; IPAddress ip; if (IPAddress.TryParse(broadcastHost, out ip)) { Medo.MessageBox.ShowInformation(this, string.Format("Broadcast IP {0} is valid.", ip.ToString())); } else { List <IPAddress> resolved = new List <IPAddress>(); foreach (var address in Dns.GetHostAddresses(broadcastHost)) { if (address.AddressFamily == AddressFamily.InterNetwork) { resolved.Add(address); } } if (resolved.Count > 0) { Medo.Text.StringAdder sb = new Medo.Text.StringAdder(", "); foreach (var address in resolved) { sb.Append(address.ToString()); } Medo.MessageBox.ShowInformation(this, string.Format("Broadcast host \"{0}\" has been resolved to {1}.", broadcastHost, sb.ToString())); } else { Medo.MessageBox.ShowWarning(this, string.Format("Cannot find valid IP address for broadcast host \"{0}\".", broadcastHost)); } } } catch (SocketException) { //No such host is known Medo.MessageBox.ShowWarning(this, string.Format("Broadcast host \"{0}\" cannot be resolved.", broadcastHost)); } finally { Cursor.Current = Cursors.Default; } }
private static string GetProperMAC(string text) { string addressText = System.Text.RegularExpressions.Regex.Replace(text.ToUpper(), "[^0-9A-F]", ":") + ":"; Medo.Text.StringAdder newText = new Medo.Text.StringAdder(":"); string lastPart = string.Empty; for (int i = 0; i < addressText.Length; i++) { switch (addressText[i]) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': lastPart += addressText.Substring(i, 1); break; case ':': while (lastPart.Length > 0) { string tmpPart = string.Empty; if (lastPart.Length > 2) { tmpPart = lastPart.Substring(0, 2); lastPart = lastPart.Substring(2, lastPart.Length - 2); } else { tmpPart = lastPart; if ((tmpPart.Length < 2) && (i < (addressText.Length - 1))) { tmpPart = tmpPart.PadLeft(2, '0'); } lastPart = string.Empty; } newText.Append(tmpPart); } break; } }//for if (newText.ToString().Length > 2 * 6 + 5) { return(newText.ToString().Substring(0, 2 * 6 + 5)); } else { return(newText.ToString()); } }