修改注册表中本地连接的Ip和网卡地址C#.docx
《修改注册表中本地连接的Ip和网卡地址C#.docx》由会员分享,可在线阅读,更多相关《修改注册表中本地连接的Ip和网卡地址C#.docx(9页珍藏版)》请在冰豆网上搜索。
修改注册表中本地连接的Ip和网卡地址C#
修改注册表中"本地连接"的Ip和网卡地址(C#)
语言c#,运行需要 .netframework2.0
在很多行业或公司,会通过限制ip的方法,使局域网内一部分ip可以上外网。
本方案通过修改ip和网卡地址达到在别人不知不觉地情况下共用一个ip上网。
==================代码如下:
=======================
usingsystem;
usingsystem.collections.generic;
usingponentmodel;
usingsystem.data;
usingsystem.drawing;
usingsystem.text;
usingsystem.windows.forms;
usingmicrosoft.win32;
usingsystem.windows;
usingsystem.management;
usingworkinformation;
usingsystem.serviceprocess;
namespacereworkmac
{
publicpartialclassform1:
form
{
publicform1()
{
initializecomponent();
}
privatevoidform1_load(objectsender,eventargse)
{
listbox1.items.clear();
registrykeymacregistry=registry.localmachine.opensubkey("system").opensubkey("currentcontrolset").opensubkey("control").opensubkey("class").opensubkey("{4d36e972-e325-11ce-bfc1-08002be10318}");//mac的注册表建所在
foreach(stringmrkinmacregistry.getsubkeynames())
{
listbox1.items.add(mrk);
}
ipglobalpropertiescomputerproperties=ipglobalproperties.getipglobalproperties();
textbox4.text=computerproperties.hostname;
networkinterface[]nics=networkinterface.getallnetworkinterfaces();
foreach(networkinterfaceadapterinnics)
{
if(adapter.name=="本地连接")
{
textbox2.text=adapter.description;
textbox3.text=adapter.getphysicaladdress().tostring();
textbox10.text=adapter.id;
}
// listbox3.items.add(adapter.id+"接口类型 "+workinterfacetype.tostring());
}
/////////////////////////////////////////////////////////////////
///一下这一段有待以后研究,现在还没有看懂
managementclassmc=newmanagementclass("win32_networkadapterconfiguration");
managementobjectcollectionmoc=mc.getinstances();
foreach(managementobjectmoinmoc)
{
if(!
(bool)mo["ipenabled"])
continue;
string[]addresses=(string[])mo["ipaddress"];
string[]subnets=(string[])mo["ipsubnet"];
foreach(stringsadinaddresses)
textbox7.text=sad;
foreach(stringsubinsubnets)
textbox8.text=sub;
}
//////////////////////////////////////////////////////////////
// checkbox2.
}
privatevoidlistbox1_selectedindexchanged(objectsender,eventargse)
{
if(listbox1.selecteditem!
=null)
{
listbox2.items.clear();
registrykeythiskey=registry.localmachine.opensubkey("system").opensubkey("currentcontrolset").opensubkey("control").opensubkey("class").opensubkey("{4d36e972-e325-11ce-bfc1-08002be10318}").opensubkey(listbox1.selecteditem.tostring());
foreach(stringthisvaluenameinthiskey.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;
}
}
privatevoidlistbox2_selectedindexchanged(objectsender,eventargse)
{
}
privatevoidcheckbox1_checkedchanged(objectsender,eventargse)
{
if(checkbox1.checked==true)
{
string_item="";
foreach(stringthisiteminlistbox1.items)
{
//listbox2.items.add(thisvaluename+" value:
"+thiskey.getvalue(thisvaluename));
registrykeythiskey=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="你还没有选择主键.";
}
}
privatevoidbutton1_click(objectsender,eventargse)
{
if(listbox1.selecteditem!
=null&&textbox5.text!
=null)//把mac写入注册表
{
registrykeythiskey=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","networkaddress");
}
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","networkaddress");
}
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!
="")
{
registrykeyipkey=registry.localmachine.opensubkey("system").opensubkey("currentcontrolset").opensubkey("services");
ipkey.opensubkey("tcpip").opensubkey("parameters").opensubkey("interfaces").opensubkey(textbox10.text,true).setvalue("ipaddress",newstring[]{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="请手动重启一下“本地连接”,就可以了";
}
}
staticvoidswitchtostatic(stringipstring,stringsubnetstring)//修改ip和子网掩码
{
managementbaseobjectinpar=null;
managementbaseobjectoutpar=null;
managementclassmc=newmanagementclass("win32_networkadapterconfiguration");
managementobjectcollectionmoc=mc.getinstances();
foreach(managementobjectmoinmoc)
{
if(!
(bool)mo["ipenabled"])
continue;
inpar=mo.getmethodparameters("enablestatic");
inpar["ipaddress"]=newstring[]{ipstring};
inpar["subnetmask"]=newstring[]{subnetstring};
outpa