c# 怎么获取本机真实的mac 地址
作者&投稿:老羽 (若有异议请与网页底部的电邮联系)
http://blog.csdn.net/zhangting1987/article/details/2732135
1.使用程序集system.management
2.使用命名空间system.management
你必须把system.management这个dll引用进你的工程中来。
不然的话找不到这个dll,也找不到引用的命名空间和类。
C#获取本机的MAC地址,参考代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
namespace _17获取MAC地址
{
class Program
{
static void Main(string[] args)
{
ManagementObjectSearcher nisc = new ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration");
foreach (ManagementObject nic in nisc.Get())
{
if (Convert.ToBoolean(nic["ipEnabled"]) == true)
{
Console.WriteLine("{0} - {1}", nic["ServiceName"], nic["MACAddress"]);
}
}
Console.ReadKey();
}
}
}
using System; using System.Collections.Generic;
using System.ComponentModel;
using System.Data; using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Management;
using System.Net.Sockets; using System.Collections; //using System.ComponentModel;using System.Windows.Forms;using System.Data;
namespace test {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string s="",mac="";
string hostInfo = Dns.GetHostName();
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
for (int i = 0; i < addressList.Length; i ++)
{
s += addressList[i].ToString(); } ManagementClass mc;
mc=new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc=mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if(mo["IPEnabled"].ToString()=="True")
mac=mo["MacAddress"].ToString();
this.textBox1.AppendText(mac + "\n\r\n\r");
}
this.Text+=hostInfo;
this.Text+=s;
this.Text+=mac;
// button1.Enabled=false; button2.Focus();
}
}
}
protected string getHostIpName()
{
string mac = "";
ManagementClass mc;
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (mo["IPEnabled"].ToString() == "True")
mac = mo["MacAddress"].ToString();
}
return mac;
}
直接调用,返回的就是
返回值是MAC地址。
public static string GetMACAddress()
{
string MoAddress = "";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
MoAddress = mo["MacAddress"].ToString();
mo.Dispose();
}
return MoAddress;
}
using System.Management;
...
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
Console.WriteLine("MAC addresst{0}", mo["MacAddress"].ToString());
mo.Dispose();
}
翠琬威赛: C#获取本机的MAC地址,参考代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Management;namespace _17获取MAC地址 {class Program{static void Main(string[] args){...
芝罘区19824288899: C#开发智能设备 怎么获取本机 的mac地址 - ?
翠琬威赛: 1、同时按Win+R,弹出运行窗口.输入CMD,按确定; 2、接在在界面中输入ipconfig /all按回车,显示本机所有的网络配置; 3、找到本机的IP对应的网卡,然后物理地址MAC就如图所示:
芝罘区19824288899: c#获取本机mac地址 - ?
翠琬威赛: 1.使用程序集system.management2.使用命名空间system.management 你必须把system.management这个dll引用进你的工程中来.不然的话找不到这个dll,也找不到引用的命名空间和类.
芝罘区19824288899: 用C#编程怎么样实现提取原始的物理MAC地址(不是更改过的MAC地址) - ?
翠琬威赛: /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox txtMac; ...
芝罘区19824288899: c#如何获取本机的本地连接mac,一定是本地连接的,mac列表不需要哦. - ?
翠琬威赛: 使用NetWorkInterface类可以获得,本地连接的Mac地址 NetworkInterface[] netWorks = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface netWork in netWorks) { Console.WriteLine(netWork.GetPhysicalAddress().ToString()); }
芝罘区19824288899: C#获取客户端MAC?
翠琬威赛: 要是客户端和服务器端都是在一个局域网内,倒是可以用arp协议得到客户端的mac地址.要是在广域网,服务器通常情况下是无法的到客户端的mac地址的,因为mac地址在局域网内终结. 当然,假如客户端和服务器端的应用层协议是你自己定义的私有协议,你可以让客户端把mac地址手动的传给服务器.
芝罘区19824288899: C#中如何得到机器的IP地址 - ?
翠琬威赛: 这两个方法是我一直在用的,都可正常获取到IP地址和Mac地址. /// <summary> /// 获取IP地址 /// </summary> /// <returns>IP地址</returns> public string GetLocalIP() { try { System.Net.NetworkInformation.NetworkInterface[] nics = System.Net....
芝罘区19824288899: c#如何跨越网段获取MAC地址信息?? - ?
翠琬威赛: 利用dns类和WMI规范获取IP及MAC地址 在C#编程中,要获取主机名和主机IP地址,是比较容易的.它提供的Dns类,可以轻松的取得主机名和IP地址.示例:string strHostName = Dns.GetHostName(); //得到本机的主机名 IPHostEntry ipEntry ...
芝罘区19824288899: 如何使用C#获取本地网关的IP和MAC地址 - ?
翠琬威赛: using System.Management; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection nics = mc.GetInstances(); foreach (ManagementObject nic in nics) { if (Convert.ToBoolean(...
芝罘区19824288899: C#如何获取客户端CPU,硬盘,MAC序列号 - ?
翠琬威赛: winform可以,webform几乎就不可能除非写插件,这是在winform下的.客户端的 using System.Runtime.InteropServices; using System.Management; using System; public class HardwareInfo {//取机器名public string GetHostName(){return ...