public void GetIPAddress() { string resultIP = string.Empty; System.Net.IPAddress[] ips = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList; foreach (System.Net.IPAddress ip in ips) { if (IsCorrentIP(ip.ToString())) { resultIP = ip.ToString(); break; } } } private bool IsCorrentIP(string ip) { string pattrn = @"(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])"; if (System.Text.RegularExpressions.Regex.IsMatch(ip, pattrn)) { return true; } else { return false; } }
|
请发表评论