posts - 43,  comments - 117,  trackbacks - 12

1.获取MAC地址操作:

 1using   System;   
 2  using   System.Runtime.InteropServices;   
 3    
 4  namespace   UtilityControl   
 5  {   
 6  ///   <summary>   
 7  ///   关于IP地址的若干操作   
 8  ///   </summary>   

 9  public   class   IP   
10  {   
11  public   IP()   
12  {   
13  //   
14  //   TODO:   在此处添加构造函数逻辑   
15  //   
16  }
   
17    
18  [DllImport("Iphlpapi.dll")]   
19  private   static   extern   int   SendARP(Int32   dest,Int32   host,ref   Int64   mac,ref   Int32   length);   
20  [DllImport("Ws2_32.dll")]     
21  private   static   extern   Int32   inet_addr(string   ip);   
22    
23  ///   <summary>   
24  ///   根据ip得到网卡mac地址   
25  ///   </summary>   
26  ///   <param   name="ip">给出的ip地址</param>   
27  ///   <returns>对应ip的网卡mac地址</returns>   

28  public   static   Int64   GetMACByIP(string   ip)     
29  {     
30  Int32   ldest=   inet_addr(ip);   //目的地的ip     
31  try     
32  {     
33  Int64   macinfo   =   new   Int64();     
34  Int32   len   =   6;     
35  int   res   =   SendARP(ldest,0,   ref   macinfo,   ref   len);     
36  return   macinfo;     
37  }
     
38  catch(Exception   err)     
39  {     
40  Console.WriteLine("Error:{0}",err.Message);     
41  }
     
42  return   0;     
43  }
     
44  }
   
45  }
 

 

-----------------------------------------------------------------

用C#编写获取远程IP,MAC的方法

 

 1[DllImport("Iphlpapi.dll")]
 2private  static  unsafe  extern  int  SendARP(Int32  dest,Int32  host,ref  IntPtr  mac,ref  IntPtr  length);
 3[DllImport("Ws2_32.dll")]
 4private  static  extern  Int32  inet_addr(string  ip);
 5
 6Int32  ldest=  inet_addr("157.60.68.163");//目的地的ip
 7Int32  lhost=  inet_addr("157.60.68.33");//本地的ip
 8
 9try
10{
11Byte[]  macinfo=new  Byte[6];
12Int32  length=6;
13
14IntPtr  mac=new  IntPtr(macinfo[0]);
15IntPtr  len=new  IntPtr(6);
16int  ii=SendARP(ldest,lhost,  ref  mac,  ref  len);
17
18Console.WriteLine("Mac  Add:"+mac);
19Console.WriteLine("length:"+len);
20
21
22}

23catch(Exception  err)
24{
25Console.WriteLine(err);
26}
  
27

 

用WMI
使用时首先添加System.Management.dll,然后引用

 1using System.Management;
 2using System.Threading; 
 3     {
 4            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
 5            ManagementObjectCollection moc = mc.GetInstances(); 
 6            foreach(ManagementObject mo in moc) 
 7            
 8                if((bool)mo["IPEnabled"== true
 9                    Response.Write("MAC address"+mo["MacAddress"].ToString()+"<br/>"); 
10                mo.Dispose(); 
11            }
 
12        }

 

2.手动设置IP或设置动态IP

如果是winform的话,需要添加对System.Management.dll的引用

然后使用下面的类就可以实现你的前2个要求:

  1namespace modify
  2{
  3    public class ChangeIP
  4    {
  5
  6        public static readonly System.Version myVersion = new System.Version(11);
  7        private ManagementBaseObject iObj = null;
  8        private ManagementBaseObject oObj = null;
  9        private ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
 10        private readonly ManagementObjectCollection moc;
 11
 12        /// <summary> 
 13        /// example: 
 14        /// <code> 
 15        /// ArLi.CommonPrj.ChangeIP o = new ArLi.CommonPrj.ChangeIP(); 
 16        /// string[] ipList = new string[]{"192.168.0.253","192.168.0.250"}; 
 17        /// string[] subnetList = new string[]{"255.255.255.0","255.255.255.0"}; 
 18        /// o.ChangeTo(ipList,subnetList); 
 19        /// </code> 
 20        /// </summary> 

 21        public ChangeIP()
 22        {
 23            moc = mc.GetInstances();
 24        }

 25
 26        /// <summary>cortrol</summary> 
 27        /// <param name="ipAddr">IPAddr List</param> 
 28        /// <param name="subnetMask">subnetMask List</param> 

 29        public void ChangeTo(string[] ipAddr, string[] subnetMask)
 30        {
 31            foreach (ManagementObject mo in moc)
 32            {
 33                if (!(bool)mo["IPEnabled"]) continue;
 34
 35                iObj = mo.GetMethodParameters("EnableStatic");
 36                iObj["IPAddress"= ipAddr;
 37                iObj["SubnetMask"= subnetMask;
 38                oObj = mo.InvokeMethod("EnableStatic", iObj, null);
 39            }

 40        }

 41
 42        /// <summary>cortrol</summary> 
 43        /// <param name="ipAddr">IPAddr List</param> 
 44        /// <param name="subnetMask">subnetMask List</param> 
 45        /// <param name="gateways">gateway List</param> 
 46        /// <param name="gatewayCostMetric">gateway CostMetric List, example: 1</param> 

 47        public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric)
 48        {
 49            foreach (ManagementObject mo in moc)
 50            {
 51                if (!(bool)mo["IPEnabled"]) continue;
 52
 53                iObj = mo.GetMethodParameters("EnableStatic");
 54                iObj["IPAddress"= ipAddr;
 55                iObj["SubnetMask"= subnetMask;
 56                oObj = mo.InvokeMethod("EnableStatic", iObj, null);
 57
 58                iObj = mo.GetMethodParameters("SetGateways");
 59                iObj["DefaultIPGateway"= gateways;
 60                iObj["GatewayCostMetric"= gatewayCostMetric;
 61                oObj = mo.InvokeMethod("SetGateways", iObj, null);
 62            }

 63        }

 64
 65        /// <summary>cortrol</summary> 
 66        /// <param name="ipAddr">IPAddr List</param> 
 67        /// <param name="subnetMask">subnetMask List</param> 
 68        /// <param name="gateways">gateway List</param> 
 69        /// <param name="gatewayCostMetric">gateway CostMetric List, example: 1</param> 
 70        /// <param name="dnsServer">DNSServer List</param> 

 71        public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric, string[] 
 72
 73dnsServer)
 74        {
 75            foreach (ManagementObject mo in moc)
 76            {
 77                if (!(bool)mo["IPEnabled"]) continue;
 78
 79                iObj = mo.GetMethodParameters("EnableStatic");
 80                iObj["IPAddress"= ipAddr;
 81                iObj["SubnetMask"= subnetMask;
 82                oObj = mo.InvokeMethod("EnableStatic", iObj, null);
 83
 84                iObj = mo.GetMethodParameters("SetGateways");
 85                iObj["DefaultIPGateway"= gateways;
 86                iObj["GatewayCostMetric"= gatewayCostMetric;
 87                oObj = mo.InvokeMethod("SetGateways", iObj, null);
 88
 89                iObj = mo.GetMethodParameters("SetDNSServerSearchOrder");
 90                iObj["DNSServerSearchOrder"= dnsServer;
 91                oObj = mo.InvokeMethod("SetDNSServerSearchOrder", iObj, null);
 92            }

 93        }

 94
 95        /// <summary> DHCPEnabled</summary> 
 96        public void EnableDHCP()
 97        {
 98            foreach (ManagementObject mo in moc)
 99            {
100                if (!(bool)mo["IPEnabled"]) continue;
101
102                if (!(bool)mo["DHCPEnabled"])
103                {
104                    iObj = mo.GetMethodParameters("EnableDHCP");
105                    oObj = mo.InvokeMethod("EnableDHCP", iObj, null);
106                }

107            }

108        }

109    }

110}

111

 


测试代码:

 1using System.Management;
 2using modify;
 3namespace WindowsApplication13
 4{
 5    public partial class Form1 : Form
 6    {
 7        public Form1()
 8