资源简介

C#修改网卡MAC地址的源码,可以随机生成mac地址,亲测可用,测试环境,windows 7 ,VS2013,稍作修改可以做成一键换地址。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Security.Permissions;
using System.Management;
using Microsoft.Win32;
using System.Net.NetworkInformation;
using System.Text.Regularexpressions;


namespace MACAddressTool
{
    public partial class Form1 : Form
    {
        public class Adapter
        {
            public Managementobject adapter;
            public string adaptername;
            public string customname;
            public int devnum;

            public Adapter(Managementobject a string aname string cname int n)
            {
                this.adapter = a;
                this.adaptername = aname;
                this.customname = cname;
                this.devnum = n;
            }

            public Adapter(NetworkInterface i) : this(i.Description) { }

            public Adapter(string aname)
            {
                this.adaptername = aname;

                var searcher = new ManagementobjectSearcher(“select * from win32_networkadapter where Name=‘“ + adaptername + “‘“);
                var found = searcher.Get();
                this.adapter = found.Castject>().FirstOrDefault();

                // Extract adapter number; this should correspond to the keys under
                // HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}
                try
                {
                    var match = Regex.Match(adapter.Path.RelativePath “\\\“(\\d+)\\\“$“);
                    this.devnum = int.Parse(match.Groups[1].Value);
                }
                catch
                {
                    return;
                }

                // Find the name the user gave to it in “Network Adapters“
                this.customname = NetworkInterface.GetAllNetworkInterfaces().Where(
                    i => i.Description == adaptername
                ).Select(
                    i => “ (“ + i.Name + “)“
                ).FirstOrDefault();
            }

            public NetworkInterface ManagedAdapter
            {
                get
                {
                    return NetworkInterface.GetAllNetworkInterfaces().Where(
                        nic => nic.Description == this.adaptername
                    ).FirstOrDefault();
                }
            }

            public string Mac
            {
                get
                {
                    try
                    {
                        return BitConverter.ToString(this.ManagedAdapter.GetPhysicalAddress().GetAddressBytes()).Replace(“-“ ““).ToUpper();
                    }
                    catch { return null; }
                }
            }

            public string RegistryKey
            {
                get
                {
                    return String.Format(@“SYSTEM\ControlSet0

评论

共有 条评论