中文字幕日韩精品一区二区免费_精品一区二区三区国产精品无卡在_国精品无码专区一区二区三区_国产αv三级中文在线

C#的網(wǎng)絡(luò)適配器操作-創(chuàng)新互聯(lián)

 網(wǎng)絡(luò)的相關(guān)設(shè)置在項(xiàng)目開(kāi)發(fā)中有較多的應(yīng)用,有時(shí)候需要在項(xiàng)目中對(duì)網(wǎng)絡(luò)信息進(jìn)行相關(guān)設(shè)置。主要包含IP設(shè)置,網(wǎng)關(guān)設(shè)置,DHCP設(shè)置,DNS設(shè)置等相關(guān)網(wǎng)絡(luò)的設(shè)置。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:國(guó)際域名空間、網(wǎng)頁(yè)空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、臥龍網(wǎng)站維護(hù)、網(wǎng)站推廣。

  現(xiàn)在提供提供幾種相關(guān)的輔助方法類(lèi)。

        /// <summary>
        /// 網(wǎng)絡(luò)適配器標(biāo)識(shí)符,如:{274F9DD5-3650-4D59-B61E-710B6AF5AB36}
        /// </summary>
        public string NetworkInterfaceId { get; set; }

        /// <summary>
        /// IP地址
        /// </summary>
        public string IpAddress
        {
            get
            {
                string ipAddress;
                var address = GetAddress();
                if (address == null)
                {
                    ipAddress = string.Empty;
                }
                else
                {
                    ipAddress = address.Address.ToString();
                }
                return ipAddress;
            }
            set { if (value == null) throw new ArgumentNullException(nameof(value)); }
        }

        /// <summary>
        /// 網(wǎng)關(guān)地址
        /// </summary>
        public string Getway
        {
            get
            {              
                string getway;
                if (Getwaryes != null && Getwaryes.Count > 0)
                {

                    getway = Getwaryes[0].Address.ToString() == "0.0.0.0" ? "" : Getwaryes[0].Address.ToString(); 
                }
                else
                {
                    getway =string.Empty;
                }
                return getway;
            }
            set { if (value == null) throw new ArgumentNullException(nameof(value)); }
        }

        /// <summary>
        /// DHCP服務(wù)器地址
        /// </summary>
        public string DhcpServer
        {
            get
            {
                string dhcpServer;
                if (DhcpServerAddresses != null && DhcpServerAddresses.Count > 0)
                {
                    dhcpServer = DhcpServerAddresses[0].ToString();
                }
                else
                {
                    dhcpServer = string.Empty;
                }
                return dhcpServer;
            }
            set { if (value == null) throw new ArgumentNullException(nameof(value)); }
        }

        /// <summary>
        /// MAC地址
        /// </summary>
        public string MacAddres
        {
            get
            {
                string macAddress;
                if (MacAddress == null)
                    macAddress = string.Empty;
                else if (MacAddress.ToString().Length == 12)
                {
                    macAddress = MacAddress.ToString().Insert(4, "-").Insert(9, "-");
                }
                else
                {
                    macAddress = MacAddress.ToString();
                }
                return macAddress;
            }
            set { if (value == null) throw new ArgumentNullException(nameof(value)); }
        }

        /// <summary>
        /// 主DNS地址
        /// </summary>
        public string DnsMain
        {
            get
            {
                var dnsMain =string.Empty;

                if (DnsAddresses.Count > 0)
                {
                    if (IsIPAddress(DnsAddresses[0].ToString()))
                    {
                        dnsMain = DnsAddresses[0].ToString();
                    }
                }
                else
                {
                    dnsMain =string.Empty;
                }
                return dnsMain;
            }
            set { if (value == null) throw new ArgumentNullException(nameof(value)); }
        }

        /// <summary>
        /// 備用DNS地址
        /// </summary>
        public string DnsBackup
        {
            get
            {
                var dnsBackup = string.Empty;
                if (DnsAddresses.Count > 1)
                {
                    if (IsIPAddress(DnsAddresses[1].ToString()))
                    {
                        dnsBackup = DnsAddresses[1].ToString();
                    }

                }
                else
                {
                    dnsBackup = string.Empty;
                }
                return dnsBackup;
            }
            set { if (value == null) throw new ArgumentNullException(nameof(value)); }
        }

        /// <summary>
        /// 子網(wǎng)掩碼
        /// </summary>
        public string Mask
        {
            get
            {
                string mask;
                var address = GetAddress();
                if (address == null)
                {
                    mask = "";
                }
                else
                {
                    if (address.IPv4Mask != null) 
                    {
                        mask = address.IPv4Mask.ToString();
                    }
                    else
                    {
                        mask = "255.255.255.0";
                    }
                   
                }
                return mask;
            }
            set { if (value == null) throw new ArgumentNullException(nameof(value)); }
        }

        /// <summary>
        /// DNS集合
        /// </summary>
        public IPAddressCollection DnsAddresses { get; set; }
        /// <summary>
        /// 網(wǎng)關(guān)地址集合
        /// </summary>
        public GatewayIPAddressInformationCollection Getwaryes { get; set; }

        /// <summary>
        /// IP地址集合
        /// </summary>
        public UnicastIPAddressInformationCollection IpAddresses { get; set; }

        /// <summary>
        /// DHCP地址集合
        /// </summary>
        public IPAddressCollection DhcpServerAddresses { get; set; }

        /// <summary>
        /// 網(wǎng)卡MAC地址
        /// </summary>
        public PhysicalAddress MacAddress { get; set; }

        /// <summary>
        /// 是否啟用DHCP服務(wù)
        /// </summary>
        public bool IsDhcpEnabled { get; set; }

        /// <summary>
        /// 描述信息
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// 網(wǎng)絡(luò)接口類(lèi)型
        /// </summary>
        /// <returns></returns>
        public string NetworkInterfaceType { get; set; }

        /// <summary>
        /// 速度
        /// </summary>
        public string Speed { get; set; } 

        /// <summary>
        /// 是否是IP地址
        /// </summary>
        /// <param name="ipAddress"></param>
        /// <returns></returns>
        public bool IsIPAddress(string ipAddress)
        {
            const string regexStr = @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";
            var regex = System.Text.RegularExpressions.Regex.Match(ipAddress, regexStr);
            return regex.Success;
        }

        /// <summary>
        /// 啟用DHCP服務(wù)
        /// </summary>
        public bool EnableDhcp()
        {
            var wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
            var moc = wmi.GetInstances();
            foreach (var o in moc)
            {
                var mo = (ManagementObject) o;
                if (!(bool)mo["IPEnabled"])
                    continue;
                if (mo["SettingID"].ToString() != NetworkInterfaceId) continue;
                mo.InvokeMethod("SetDNSServerSearchOrder", null);
                mo.InvokeMethod("EnableDHCP", null);
            }
            //查詢現(xiàn)適配器接口信息
            var networkAdapter = new NetworkAdapterUtil().GetNeworkAdapterByNetworkInterfaceId(NetworkInterfaceId); 
            return networkAdapter != null && networkAdapter.IsDhcpEnabled;
        }

        /// <summary>
        /// 設(shè)置IP地址,子網(wǎng)掩碼,網(wǎng)關(guān),DNS,
        /// </summary>
        public bool SetIpAddressSubMaskDnsGetway(string ipAddress, string subMask, string getWay, string dnsMain, string dnsBackup)
        {
            string[] dnsArray;
            if (string.IsNullOrEmpty(dnsBackup))
            {
                dnsArray = new[] { dnsMain };
            }
            else
            {
                dnsArray = new[] { dnsMain, dnsBackup };
            }
            return SetIpAddress(new[] { ipAddress }, new[] { subMask }, new[] { getWay }, dnsArray);

        }

        /// <summary>
        /// 設(shè)置IP地址和子網(wǎng)掩碼
        /// </summary>
        public bool SetIpAddressAndSubMask(string ipAddress, string subMask)
        {
           return SetIpAddress(new[] { ipAddress }, new[] { subMask }, null, null);

        }


        /// <summary>
        /// 設(shè)置IP網(wǎng)關(guān)
        /// </summary>
        public bool SetGetWayAddress(string getWay)
        {
            return SetIpAddress(null, null, new[] { getWay }, null);

        }

        /// <summary>
        /// 設(shè)置主,備份DNS地址
        /// </summary>
        public bool SetDnsAddress(string dnsMain, string dnsBackup)
        {
            string[] dnsArray;
            if (string.IsNullOrEmpty(dnsBackup))
            {
                dnsArray = new[] { dnsMain };
            }
            else
            {
                dnsArray = new[] { dnsMain, dnsBackup };
            }
            return  SetIpAddress(null, null, null, dnsArray);

        }
        

        /// <summary>
        /// 得到IPV4地址
        /// </summary>
        /// <returns></returns>
        private UnicastIPAddressInformation GetAddress()
        {
            if (IpAddresses == null || IpAddresses.Count <= 0) return null;
            switch (IpAddresses.Count)
            {
                case 3:
                    return IpAddresses[2];
                case 2:
                    return IpAddresses[1];
            }
            return IpAddresses[0];
        }


        /// <summary>
        /// 檢查設(shè)置IP地址,如果返回空,表示檢查通過(guò),為了方便返回就字符串了,沒(méi)用枚舉了
        /// </summary>
        /// <param name="ipAddress"></param>
        /// <param name="subMask"></param>
        /// <param name="getWay"></param>
        /// <param name="dnsMain"></param>
        /// <param name="dnsBackup"></param>
        /// <returns></returns>
        public string IsIPAddress(string ipAddress, string subMask, string getWay, string dnsMain, string dnsBackup)
        {
            if (!string.IsNullOrEmpty(ipAddress))
            {
                if (!IsIPAddress(ipAddress)) return "IP地址格式不對(duì)";
            }
           if (!string.IsNullOrEmpty(subMask))
            {
                if (!IsIPAddress(subMask)) return "子網(wǎng)掩碼格式不對(duì)";
            }
           if (!string.IsNullOrEmpty(getWay))
            {
                if (!IsIPAddress(getWay)) return "網(wǎng)關(guān)地址格式不對(duì)";
            }
            if (!string.IsNullOrEmpty(dnsMain))
            {
                if (!IsIPAddress(dnsMain)) return "主DNS地址格式不對(duì)";
            }
            if (string.IsNullOrEmpty(dnsBackup)) return "";
            return !IsIPAddress(dnsBackup) ? "備用DNS地址格式不對(duì)" : "";
        }



        /// <summary>
        /// 設(shè)置IP地址
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="submask"></param>
        /// <param name="getway"></param>
        /// <param name="dns"></param>
        private bool SetIpAddress(IEnumerable ip, IEnumerable submask, IEnumerable getway, IEnumerable dns)
        {
            var wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
            var moc = wmi.GetInstances();
            foreach (var o in moc)
            {
                var mo = (ManagementObject) o;
                if (!(bool)mo["IPEnabled"])
                    continue;
                if (NetworkInterfaceId != mo["SettingID"].ToString()) continue;
                ManagementBaseObject inPar;
                ManagementBaseObject outPar;
                string str = null;
                if (ip != null && submask != null)
                {
                    inPar = mo.GetMethodParameters("EnableStatic");
                    inPar["IPAddress"] = ip;
                    inPar["SubnetMask"] = submask;
                    outPar = mo.InvokeMethod("EnableStatic", inPar, null);
                    if (outPar != null) str = outPar["returnvalue"].ToString();
                    return (str=="0"||str=="1");
                    //獲取操作設(shè)置IP的返回值, 可根據(jù)返回值去確認(rèn)IP是否設(shè)置成功。 0或1表示成功
                }
                if(getway!=null)
                {
                    inPar = mo.GetMethodParameters("SetGateways");
                    inPar["DefaultIPGateway"] = getway;
                    outPar = mo.InvokeMethod("SetGateways", inPar, null);
                    if (outPar != null) str = outPar["returnvalue"].ToString();
                    return (str == "0" || str == "1");
                }
                if (dns == null) continue;
                inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
                inPar["DNSServerSearchOrder"] = dns;
                outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
                if (outPar != null) str = outPar["returnvalue"].ToString();
                return (str == "0" || str == "1");
            }
            return false;
        }

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開(kāi)啟,新人活動(dòng)云服務(wù)器買(mǎi)多久送多久。

本文題目:C#的網(wǎng)絡(luò)適配器操作-創(chuàng)新互聯(lián)
分享鏈接:http://www.rwnh.cn/article12/csepgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站維護(hù)、響應(yīng)式網(wǎng)站、虛擬主機(jī)、靜態(tài)網(wǎng)站網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)
时尚| 吴忠市| 民乐县| 綦江县| 凤翔县| 长子县| 巫溪县| 南昌县| 嘉峪关市| 息烽县| 柯坪县| 都安| 集安市| 泌阳县| 泗阳县| 镶黄旗| 山阳县| 嘉黎县| 津南区| 大悟县| 天柱县| 阜宁县| 舞阳县| 新源县| 武功县| 木兰县| 新安县| 仪征市| 宾阳县| 汉寿县| 左云县| 五指山市| 田林县| 吕梁市| 长治县| 天峨县| 灌阳县| 个旧市| 拉萨市| 神农架林区| 石渠县|