C#修改Mac地址

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 Shell32;
using System.Threading;
using System.IO;

namespace WindowsFormsApplication2
{
    public partial class MacModifyForm : Tokay.SystemUI.Dialogs.BasicDialog
    {
        private const string NETWORKADDRESS_KEY = @"SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\0007";
        private const string NETWORKADDRESS_NAME = "NetworkAddress";

        private const string discVerb = "禁用(&B)";
        private const string connVerb = "启用(&A)";

        private readonly string bufferFile = Application.StartupPath + "\\buffer.dat";
        private readonly List<string> bufferIp = new List<string>();

        public MacModifyForm()
        {
            InitializeComponent();

            if (File.Exists(bufferFile))
            {
                StreamReader sr = new StreamReader(bufferFile);
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    if (line.Length > 0)
                    {
                        bufferIp.Add(line.Trim());
                    }
                }
            }
        }

        private void MacModifyForm_Load(object sender, EventArgs e)
        {
            this.textBox1.Text = ReadMac();
            this.btnOK.Enabled = false;
        }

        private string ReadMac()
        {
            RegistryKey HKLM = Registry.LocalMachine;
            try
            {
                RegistryKey Reg = HKLM.OpenSubKey(NETWORKADDRESS_KEY);
                object o = Reg.GetValue(NETWORKADDRESS_NAME);
                return o.ToString();
            }
            catch
            {
                return string.Empty;
            }
        }

        private void SetMac(string mac)
        {
            RegistryKey HKLM = Registry.LocalMachine;
            RegistryKey Reg = HKLM.OpenSubKey(NETWORKADDRESS_KEY, true);
            Reg.SetValue(NETWORKADDRESS_NAME, mac);
        }

        private void RestartMac()
        {
            this.StopMac();
            Thread.Sleep(3000);
            this.StartMac();
        }

        private void StartMac()
        {
            Shell sh = new Shell32.Shell();
            Folder folder;
            Folder fd;
            folder = sh.NameSpace(3);
            foreach (FolderItem myItem in folder.Items())
            {
                if (myItem.Name == "网络连接")
                {
                    fd = (Folder)myItem.GetFolder;
                    //禁用网络
                    foreach (FolderItem fi in fd.Items())
                    {
                        foreach (FolderItemVerb Fib in fi.Verbs())
                        {
                            //启用网络
                            if (Fib.Name == connVerb)
                            {
                                Fib.DoIt();
                                break;
                            }
                        }
                    }
                }
            }
        }

        private void StopMac()
        {
            Shell sh = new Shell32.Shell();
            Folder folder;
            Folder fd;
            folder = sh.NameSpace(3);
            foreach (FolderItem myItem in folder.Items())
            {
                if (myItem.Name == "网络连接")
                {
                    fd = (Folder)myItem.GetFolder;
                    //禁用网络
                    foreach (FolderItem fi in fd.Items())
                    {
                        foreach (FolderItemVerb Fib in fi.Verbs())
                        {
                            if (Fib.Name == discVerb)
                            {
                                Fib.DoIt();
                                break;
                            }
                        }
                    }
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                this.btnOK.Enabled = false;
                this.Cursor = Cursors.WaitCursor;

                //设置Mac到注册表
                this.SetMac(this.textBox1.Text);
                //重新启用网络
                this.RestartMac();

                this.Hide();

                if (!this.bufferIp.Contains(this.textBox1.Text))
                {
                    this.bufferIp.Add(this.textBox1.Text);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            this.btnOK.Enabled = this.textBox1.Text.Length == 17;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 显示主窗体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Visible = true;
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            this.显示主窗体ToolStripMenuItem.Enabled = !this.Visible;
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Visible = !this.Visible;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                this.Visible = false;
                this.timer1.Enabled = false;
            }
        }

        private void MacModifyForm_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                this.textBox1.Items.Clear();

                foreach (string s in this.bufferIp)
                {
                    this.textBox1.Items.Add(s);
                }
            }
        }

        private void MacModifyForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                if (File.Exists(this.bufferFile))
                {
                    FileInfo fi = new FileInfo(this.bufferFile);
                    fi.Attributes = FileAttributes.Normal;
                    File.Delete(this.bufferFile);
                }

                StringBuilder sb = new StringBuilder();
                foreach (string s in this.bufferIp)
                {
                    sb.AppendLine(s);
                }
                File.WriteAllText(this.bufferFile, sb.ToString(), Encoding.Default);
            }
            catch { }
        }
    }
}

  

posted @ 2013-03-05 08:20  程序之魂  阅读(1117)  评论(0编辑  收藏  举报