分享

class IpMac

 缘木求鱼001 2012-09-29
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, 0ref pMacAddr, ref PhyAddrLen);
                byte[] _Bytes1 = BitConverter.GetBytes(pMacAddr);
                mac = BitConverter.ToString(_Bytes1, 06);
                return mac;
            }
            catch
            {
                return null;
            }
        }
 
        public static bool IsIpInt(string intstr)
        {
            return IsIntInScale(intstr, 0254);
        }
 
        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);
            }
        }
 
        
 
 
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多