取得 IP, Mac 資料

1 篇文章 / 0 new
author
取得 IP, Mac 資料
依主機名稱取得
string strHostName = Dns.GetHostName();  //取得主機名稱
IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //主機IP
string ip = ipEntry.AddressList[0].ToString(); //假設為單網卡
//多網卡時可用 foreach (IPAddress addr in ipHostEntry.AddressList)
依IP取得主機名稱則使用 Dns.GetHostByAddress()

依IP取得網卡MAC
string mac = "";
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "nbtstat";
p.StartInfo.Arguments = "-a " + "IP 位置";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
int len = output.IndexOf("MAC Address = ");
if (len > 0)
{
    mac = output.Substring(len + 14, 17);
}
p.WaitForExit();
MessageBox.Show(mac);
Free Web Hosting