国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

修改注冊表中"本地連接"的Ip和網卡地址(C#)

2024-07-21 02:25:53
字體:
來源:轉載
供稿:網友

語言c# ,運行需要 .net framework 2.0

在很多行業或公司,會通過限制ip的方法,使局域網內一部分ip可以上外網。本方案通過修改ip和網卡地址達到在別人不知不覺地情況下共用一個ip上網。
==================代碼如下: =======================
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
using microsoft.win32;
using system.windows;
using system.management;
using system.net.networkinformation;
using system.serviceprocess;

namespace reworkmac
{
    public partial class form1 : form
    {
        public form1()
        {
            initializecomponent();
        }

        private void form1_load(object sender, eventargs e)
        {
            listbox1.items.clear();
            registrykey macregistry = registry.localmachine.opensubkey("system").opensubkey("currentcontrolset").opensubkey("control").opensubkey("class").opensubkey("{4d36e972-e325-11ce-bfc1-08002be10318}");//mac的注冊表建所在
            foreach (string mrk in macregistry.getsubkeynames())
            {
                listbox1.items.add(mrk);
            }
            ipglobalproperties computerproperties = ipglobalproperties.getipglobalproperties();
           
            textbox4.text = computerproperties.hostname;
            networkinterface[] nics = networkinterface.getallnetworkinterfaces();
            foreach (networkinterface adapter in nics)
            {
                if (adapter.name=="本地連接")
                {
                    textbox2.text = adapter.description;
                    textbox3.text = adapter.getphysicaladdress().tostring();
                    textbox10.text = adapter.id;
                }
        //        listbox3.items.add(adapter.id+" 接口類型  "+adapter.networkinterfacetype.tostring());
            }
            /////////////////////////////////////////////////////////////////
            ///一下這一段有待以后研究,現在還沒有看懂
            managementclass mc = new managementclass("win32_networkadapterconfiguration");
            managementobjectcollection moc = mc.getinstances();
            foreach (managementobject mo in moc)
            {
                if (!(bool)mo["ipenabled"])
                    continue;
                string[] addresses = (string[])mo["ipaddress"];
                string[] subnets = (string[])mo["ipsubnet"];
                foreach (string sad in addresses)
                    textbox7.text = sad;
                foreach (string sub in subnets)
                    textbox8.text = sub;
            }
            //////////////////////////////////////////////////////////////
         //   checkbox2.
        }
        private void listbox1_selectedindexchanged(object sender, eventargs e)
        {
            if (listbox1.selecteditem != null)
            {
                listbox2.items.clear();
                registrykey thiskey = registry.localmachine.opensubkey("system").opensubkey("currentcontrolset").opensubkey("control").opensubkey("class").opensubkey("{4d36e972-e325-11ce-bfc1-08002be10318}").opensubkey(listbox1.selecteditem.tostring());
                foreach (string thisvaluename in thiskey.getvaluenames())
                {
                    listbox2.items.add(thisvaluename + "    該子鍵的值: " + thiskey.getvalue(thisvaluename));
                }
                if (thiskey.getvalue("driverdesc") != null)
                {
                    textbox1.text = thiskey.getvalue("driverdesc").tostring();
                }
                else
                {
                    textbox1.text = "此主建下沒有driverdesc項";
                }
                button1.text = "更改mac及ip(注意:更改之前請自己備份相關數據)";
                checkbox1.backcolor = this.backcolor;
                button1.backcolor = color.transparent;
                label5.backcolor = this.label1.backcolor;
            }
        }

        private void listbox2_selectedindexchanged(object sender, eventargs e)
        {

        }

        private void checkbox1_checkedchanged(object sender, eventargs e)
        {
            if (checkbox1.checked == true)
            {
                string _item = "";
                foreach (string thisitem in listbox1.items)
                {

                    //listbox2.items.add(thisvaluename + "    value:" + thiskey.getvalue(thisvaluename));
                    registrykey thiskey = registry.localmachine.opensubkey("system").opensubkey("currentcontrolset").opensubkey("control").opensubkey("class").opensubkey("{4d36e972-e325-11ce-bfc1-08002be10318}");
                    if (thiskey.opensubkey(thisitem).getvalue("netcfginstanceid") != null && thiskey.opensubkey(thisitem).getvalue("netcfginstanceid").tostring() == textbox10.text)//
                    {
                        _item = thisitem;
                    }
                }
                listbox1.selecteditem = _item;
                button1.text = "更改mac及ip(注意:更改之前請自己備份相關數據)";
                checkbox1.backcolor =this.backcolor;
                button1.backcolor = color.transparent;
                label5.backcolor = this.label1.backcolor;
            }
            else
            {
                listbox1.clearselected();
                listbox2.items.clear();
                textbox1.text = "你還沒有選擇主鍵.";
            }
        }
        private void button1_click(object sender, eventargs e)
        {
           
            if (listbox1.selecteditem != null && textbox5.text!=null)//把mac寫入注冊表
            {
                registrykey thiskey = registry.localmachine.opensubkey("system").opensubkey("currentcontrolset").opensubkey("control").opensubkey("class").opensubkey("{4d36e972-e325-11ce-bfc1-08002be10318}").opensubkey(listbox1.selecteditem.tostring(), true);
                if (thiskey.getvalue("networkaddress") == null)
                {
                    thiskey.setvalue("networkaddress", (object)textbox5.text);
                    thiskey.opensubkey("ndi", true).opensubkey("params", true).opensubkey("networkaddress", true).setvalue("default", (object)textbox5.text);
                    thiskey.opensubkey("ndi", true).opensubkey("params", true).opensubkey("networkaddress", true).setvalue("paramdesc", "network address");
                }
                else
                {
                    thiskey.setvalue("networkaddress", (object)textbox5.text);
                    thiskey.opensubkey("ndi", true).opensubkey("params", true).opensubkey("networkaddress", true).setvalue("default", (object)textbox5.text);
                    thiskey.opensubkey("ndi", true).opensubkey("params", true).opensubkey("networkaddress", true).setvalue("paramdesc", "network address");
                }
                if (thiskey.getvalue("networkaddress").tostring() == textbox5.text)
                {
                    checkbox3.visible=true;
                    checkbox3.text="修改成功!";
                    checkbox3.checked=true;
                }
                else
                {
                    checkbox3.visible = true;
                    checkbox3.text = "修改失敗!";
                    checkbox3.checked = false;
                    checkbox3.backcolor = color.red;
                    return;
                }
            }
            else
            {
                if (textbox5.text == "")
                {
                    textbox5.text = "請在此處輸入mac地址";
                }
                button1.text = "請選擇網卡所對應的主鍵!";
                checkbox1.backcolor = color.lightblue;
                button1.backcolor = color.lightblue;
                label5.backcolor = color.lightblue;
            }
            /////////////////////////////////修改ip   
            if (textbox6.text != "")
            {
                registrykey ipkey = registry.localmachine.opensubkey("system").opensubkey("currentcontrolset").opensubkey("services");
                ipkey.opensubkey("tcpip").opensubkey("parameters").opensubkey("interfaces").opensubkey(textbox10.text, true).setvalue("ipaddress", new string[] { textbox6.text }, registryvaluekind.multistring);//注意此處registryvaluekind.multistring的用法
 /////////////////////////////////////////////驗證
                if (ipkey.opensubkey("tcpip").opensubkey("parameters").opensubkey("interfaces").opensubkey(textbox10.text, true).getvalue("ipaddress").tostring() == textbox6.text)
                {
                    checkbox4.visible = true;
                    checkbox4.text = "修改成功!";
                    checkbox4.checked = true;
                }
                else
                {
                    checkbox4.visible = true;
                    checkbox4.text = "修改失敗!";
                    checkbox4.checked = false;
                    checkbox4.backcolor = color.red;
                    return;
                }
            }
            else
            {
                textbox6.text = "請在此處輸入ip地址";
            }
            ///////////////////////////////////////
            if (checkbox3.checked && checkbox4.checked)
            {
                label13.visible = true;
                label13.text = "請手動重啟一下“本地連接”,就可以了";
            }              
        }
        static void switchtostatic(string ipstring,string subnetstring)//修改ip和子網掩碼
        {
            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;

                inpar = mo.getmethodparameters("enablestatic");
                inpar["ipaddress"] = new string[] { ipstring };
                inpar["subnetmask"] = new string[] { subnetstring };
                outpar = mo.invokemethod("enablestatic", inpar, null);
                break;
            }
        }
        static void reportip()
        {
            managementclass mc = new managementclass("win32_networkadapterconfiguration");
            managementobjectcollection moc = mc.getinstances();
            foreach (managementobject mo in moc)
            {
                if (!(bool)mo["ipenabled"])
                    continue;

                console.writeline("{0}/n   svc:   '{1}'   mac:   [{2}]", (string)mo["caption"],
                  (string)mo["servicename"], (string)mo["macaddress"]);

                string[] addresses = (string[])mo["ipaddress"];
                string[] subnets = (string[])mo["ipsubnet"];

                console.writeline("   addresses   :");
                foreach (string sad in addresses)
                    console.writeline("/t'{0}'", sad);

                console.writeline("   subnets   :");
                foreach (string sub in subnets)
                    console.writeline("/t'{0}'", sub);
            }
        }

        private void checkbox2_checkedchanged(object sender, eventargs e)
        {
            if (checkbox2.checked == true)
            {
                textbox9.text = textbox8.text;
                textbox9.readonly = true;
            }
            else
            {
                textbox9.clear();
                textbox9.readonly = false;
            }
        }

        private void button2_click(object sender, eventargs e)
        {
            if (textbox6.text != "" && textbox9.text != "")
            {
                switchtostatic(textbox6.text, textbox9.text);
            }
            else
            {
                if (textbox6.text == "")
                {
                    textbox6.text = "請在此處輸入ip地址";
                }
                if (textbox9.text == "")
                {
                    textbox9.text = "請在此處輸入子網掩碼";
                }
            }
        }

         private void button4_click(object sender, eventargs e)
        {
                button4.text = "暫時不提供本功能!";
        }
        private void textbox5_click(object sender, eventargs e)
        {
            if (textbox5.text == "請在此處輸入mac地址")
            textbox5.clear();
        }
        private void textbox6_click(object sender, eventargs e)
        {
            if (textbox6.text == "請在此處輸入ip地址")
                textbox6.clear();
        }
        private void textbox9_click(object sender, eventargs e)
        {
            if (textbox9.text == "請在此處輸入子網掩碼")
                textbox9.clear();
        }

        private void button3_click(object sender, eventargs e)
        {
            listbox1.items.clear();
            registrykey macregistry = registry.localmachine.opensubkey("system").opensubkey("currentcontrolset").opensubkey("control").opensubkey("class").opensubkey("{4d36e972-e325-11ce-bfc1-08002be10318}");//mac的注冊表建所在
            foreach (string mrk in macregistry.getsubkeynames())
            {
                listbox1.items.add(mrk);
            }
            ipglobalproperties computerproperties = ipglobalproperties.getipglobalproperties();
           
            textbox4.text = computerproperties.hostname;
            networkinterface[] nics = networkinterface.getallnetworkinterfaces();
            foreach (networkinterface adapter in nics)
            {
                if (adapter.name == "本地連接")
                {
                    textbox2.text = adapter.description;
                    textbox3.text = adapter.getphysicaladdress().tostring();
                    textbox10.text = adapter.id;
                }
            }
            /////////////////////////////////////////////////////////////////
            managementclass mc = new managementclass("win32_networkadapterconfiguration");
            managementobjectcollection moc = mc.getinstances();
            foreach (managementobject mo in moc)
            {
                if (!(bool)mo["ipenabled"])
                    continue;
                string[] addresses = (string[])mo["ipaddress"];
                string[] subnets = (string[])mo["ipsubnet"];
                foreach (string sad in addresses)
                    textbox7.text = sad;
                foreach (string sub in subnets)
                    textbox8.text = sub;
            }

        }
    }   
}

================截圖=====================

還沒有生成的樣子。


生成后的樣子。

上一篇:ini文件讀取(C#)

下一篇:C#截屏

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莆田市| 绥宁县| 平遥县| 鄂温| 库伦旗| 融水| 丹棱县| 宁河县| 清流县| 老河口市| 延庆县| 永善县| 苍南县| 阳高县| 嵊州市| 马鞍山市| 沙湾县| 永德县| 巴彦县| 武乡县| 宣汉县| 缙云县| 容城县| 奈曼旗| 灵宝市| 原平市| 保靖县| 福泉市| 额尔古纳市| 仁寿县| 南充市| 松滋市| 万盛区| 双峰县| 德格县| 松江区| 婺源县| 潮州市| 景东| 长宁区| 汝州市|