程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> ·修改注冊表中“本地連接”的Ip和網卡地址(C#)

·修改注冊表中“本地連接”的Ip和網卡地址(C#)

編輯:關於C語言

語言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;
            }

        }
    }   
}

================截圖=====================

還沒有生成的樣子。


生成後的樣子。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved