语言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;
}
}
}
}
================截图=====================
还没有生成的样子。
生成后的样子。
新闻热点
疑难解答