依主機名稱取得
依IP取得網卡MAC
依IP取得主機名稱則使用 Dns.GetHostByAddress()string strHostName = Dns.GetHostName(); //取得主機名稱 IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //主機IP string ip = ipEntry.AddressList[0].ToString(); //假設為單網卡 //多網卡時可用 foreach (IPAddress addr in ipHostEntry.AddressList)
依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);