1、修改注册表中本地连接的Ip和网卡地址C#修改注册表中本地连接的Ip和网卡地址(C#)语言c# ,运行需要.net framework 2.0在很多行业或公司,会通过限制ip的方法,使局域网内一部分ip可以上外网。本方案通过修改ip和网卡地址达到在别人不知不觉地情况下共用一个ip上网。=代码如下: =using system;using system.collections.generic;using ponentmodel;using system.data;using system.drawing;using system.text;using system.windows.forms;us
2、ing microsoft.win32;using system.windows;using system.management;using workinformation;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 macre
3、gistry = 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 = ipgl
4、obalproperties.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()
5、.tostring(); textbox10.text = adapter.id; / listbox3.items.add(adapter.id+ 接口类型 +workinterfacetype.tostring(); / /一下这一段有待以后研究,现在还没有看懂 managementclass mc = new managementclass(win32_networkadapterconfiguration); managementobjectcollection moc = mc.getinstances(); foreach (managementobject mo in moc)
6、if (!(bool)moipenabled) continue; string addresses = (string)moipaddress; string subnets = (string)moipsubnet; foreach (string sad in addresses) textbox7.text = sad; foreach (string sub in subnets) textbox8.text = sub; / / checkbox2. private void listbox1_selectedindexchanged(object sender, eventarg
7、s 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 (st
8、ring 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(注意:更改之前请自己备份相关
9、数据); 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 _i
10、tem = ; 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-0800
11、2be10318); 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; butt
12、on1.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
13、= 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)te
14、xtbox5.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(networka
15、ddress, (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.
16、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 = 请在此处输入地址; b
17、utton1.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(t
18、cpip).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.t
19、ext, 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 (che
20、ckbox3.checked & checkbox4.checked) label13.visible = true; label13.text = 请手动重启一下“本地连接”,就可以了; static void switchtostatic(string ipstring,string subnetstring)/修改和子网掩码 managementbaseobject inpar = null; managementbaseobject outpar = null; managementclass mc = new managementclass(win32_networkadapterconfiguration); managementobjectcollection moc = mc.getinstances(); foreach (managementobject mo in moc) if (!(bool)moipenabled) continue; inpar = mo.getmethodparameters(enablestatic); inparipaddress = new string ipstring ; inparsubnetmask = new string subnetstring ; outpa
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1