using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
namespace WindowsFormsApplication18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCheck_Click(object sender, EventArgs e)
{
string ipAddress = this.comboBox1.Text.Trim();
String str = comboBox1.Text;//获取下拉框内容
labelIpResult.Text = str;//设置为label内容
Regex rgx = new Regex(@"^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$");
if (rgx.IsMatch(ipAddress))
{
this.labelIpResult.Text = GetName(ipAddress);
}
else
{
this.labelIpResult.Text = "IP 无效.";
}
}
//获取所有局域网机器名字
private void GetAllLocalMachineName()
{
System.Diagnostics.ProcessStartInfo prcssStrtInfo = new System.Diagnostics.ProcessStartInfo();
prcssStrtInfo.FileName = @"C:\windows\system32\cmd.exe";
prcssStrtInfo.Arguments = @"/c net view > E:\\LanMachineName.txt"; // 将所有局域网机器名输出到文本文件
prcssStrtInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process.Start(prcssStrtInfo);
}
//通过ip获取主机名字
private string GetName(string IpAddress)
{
string name="";
try{
IPHostEntry ipHstEntry= Dns.GetHostByAddress(IpAddress);
name=ipHstEntry.HostName.ToString();//获取主机名字
}
catch(Exception e)
{
name=e.Message;
}
return name;
}
}
}
这运行下来一直无效,没有ip和机器名字