语言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;
}
}
}
}
================截图=====================
还没有生成的样子。
生成后的样子。