C#windows版本判断+启用禁用本地连接+修改mac地址.txt
windows版本判断+启用禁用本地连接+修改mac地址
int _majorVersion;
int _minorVersion;
int _revision;
int _buildNumber;
string _versionNumber;
SystemOS _osID;
public void GetOsVersion()
{
OperatingSystem os = Environment.OSVersion;
Version vs = os.Version;
this._majorVersion = vs.Major;
this._minorVersion = vs.Minor;
this._revision = vs.Revision;
this._buildNumber = vs.Build;
this._versionNumber = this._majorVersion.ToString() + ":" +
this._minorVersion.ToString() + ":" + this._revision.ToString() +
":" + this._buildNumber.ToString();
if (os.Platform == PlatformID.Win32NT && vs.Major == 5)
{
if (vs.Minor > 0) { this._osID = SystemOS.WinXP; }
else { this._osID = SystemOS.Win2K; }
}
else if (os.Platform == PlatformID.Win32NT && vs.Major == 6)
{
if (vs.Minor > 0) { this._osID = SystemOS.Win7; }
else { this._osID = SystemOS.WinVista; }
}
else if (os.Platform == PlatformID.Win32NT && vs.Major <= 4)
{
this._osID = SystemOS.WinNT;
}
else if (os.Platform == PlatformID.Win32Windows)
{
if (vs.Major >= 4 && vs.Minor > 0)
{
if (vs.Minor < 90)
{
this._osID = SystemOS.Win98;
}
else
{
this._osID = SystemOS.WinME;
}
}
else
{
this._osID = SystemOS.Win95;
}
}
else if (os.Platform == PlatformID.Win32S)
{
this._osID = SystemOS.Win32s;
}
}
//------------------------------------------------------------------------------
---------------------
///
///Define different Operating Systems
///
public enum SystemOS
{
WinErr = 0, //Error
Win95 = 1,
Win98 = 2,
WinME = 3,
WinNT = 4,
Win2K = 5,
WinXP = 6,
Win32s = 7,
WinVista = 8,
Win7 = 9
}
这些都写完了,我要做什么,明白了吧.
语言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").OpenS
ubKey("Control").OpenSubKey("Class").OpenSubKey("{4D36E972-E325-11CE-BFC1-08002b
E10318}");//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").OpenS
ubKey("Control").OpenSubKey("Class").OpenSubKey("{4D36E972-E325-11CE-BFC1-08002b
E10318}").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").OpenS
ubKey("Control").OpenSubKey("Class").OpenSubKey("{4D36E972-E325-11CE-BFC1-08002b
E10318}");
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 = "请在此处输入,,;地址";
}
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").OpenS
ubKey("Services");
ipKey.OpenSubKey("Tcpip").OpenSubKey("Parameters").OpenSubKey("Interfaces").Open
SubKey(textBox10.Text, true).SetValue("IPAddress", new string[] { textBox6.Text },
RegistryValueKind.MultiString);//注意此处RegistryValueKind.MultiString的用法
/////////////////////////////////////////////验证
if
(ipKey.OpenSubKey("Tcpip").OpenSubKey("Parameters").OpenSubKey("Interfaces").Ope
nSubKey(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)//修改,,
和子网掩码
{
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 == "请在此处输入,,;地址")
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").OpenS
ubKey("Control").OpenSubKey("Class").OpenSubKey("{4D36E972-E325-11CE-BFC1-08002b
E10318}");//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;
}
}
}
}