1 关于域名解析 作用:可以把域名转换成IP地址
using System.Net;
public static string domain2ip(string str)
{
string _return = "";
try
{
IPHostEntry hostinfo = Dns.GetHostByName(str);
IPAddress[] aryIP = hostinfo.AddressList;
_return = aryIP[0].ToString();
}
catch (Exception e)
{
_return = e.Message;
}
return _return;
}
2 正则式匹配 作用:验证一个字串是否符合IP规则
using System.Text.RegularExpressions;
Match m = Regex.Match(ip, "(//d{1,3}//.){3}//d{1,3}");
if (m.Success)
data_ip = ip;//IP合法
else
{ //IP不合法
lab_prompt.Text = "服务器连接失败...";
this.btn_login.Enabled = false;
}
本文介绍了如何使用C#实现域名到IP地址的解析,并提供了一种验证IP地址格式正确性的方法。通过DNS.GetHostByName方法获取主机信息并提取IP地址,利用正则表达式检查IP地址的有效性。
4919

被折叠的 条评论
为什么被折叠?



