using System; using System.Management; using System.Net; using System.Runtime.InteropServices; namespace Xarp { public static class IpMac { [DllImport("Iphlpapi.dll")] public static extern uint SendARP(uint DestIP, uint SrcIP, ref ulong pMacAddr, ref uint PhyAddrLen); public static string GetMac(string ip) { string mac = null; IPAddress Address; if (!IPAddress.TryParse(ip, out Address)) return null; uint DestIP = BitConverter.ToUInt32(Address.GetAddressBytes(), 0); ulong pMacAddr = 0; uint PhyAddrLen = 6; try { uint error_code = SendARP(DestIP, 0, ref pMacAddr, ref PhyAddrLen); byte[] _Bytes1 = BitConverter.GetBytes(pMacAddr); mac = BitConverter.ToString(_Bytes1, 0, 6); return mac; } catch { return null; } } public static bool IsIpInt(string intstr) { return IsIntInScale(intstr, 0, 254); } public static bool IsIntInScale(string intstr, int min, int max) { try { int i = int.Parse(intstr); if (i < min || i > max) { return false; } else return true; } catch { return false; } } public static string FormatMac(string s) { for (int i = 10; i > 0; i = i - 2) { s = s.Insert(i, "-"); } return s; } public static void SetIp(string ip, string submask, string getway, string dns1, string dns2) { SetIp(new string[] { ip }, new string[] { submask }, new string[] { getway }, new string[] { dns1, dns2 }); } public static void SetIp(string ip, string submask, string getway, string dns) { SetIp(new string[] { ip }, new string[] { submask }, new string[] { getway }, new string[] { dns }); } public static void SetIp(string[] ip, string[] submask, string[] getway, string[] dns) { ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = wmi.GetInstances(); ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; foreach (ManagementObject mo in moc) { if (!(bool)mo["IPEnabled"]) continue; if (ip != null && submask != null) { inPar = mo.GetMethodParameters("EnableStatic"); inPar["IPAddress"] = ip; inPar["SubnetMask"] = submask; outPar = mo.InvokeMethod("EnableStatic", inPar, null); } if (getway != null) { inPar = mo.GetMethodParameters("SetGateways"); inPar["DefaultIPGateway"] = getway; outPar = mo.InvokeMethod("SetGateways", inPar, null); } if (dns != null) { inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); inPar["DNSServerSearchOrder"] = dns; outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); } } } public static void EnableDhcp() { ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (!(bool)mo["IPEnabled"]) continue; //启用DHCP inPar = mo.GetMethodParameters("EnableDHCP"); outPar = mo.InvokeMethod("EnableDHCP", inPar, null); //重置DNS inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); inPar["DNSServerSearchOrder"] = null; outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); } } } } |
|