最强版C#获取机器IP MAC信息

/*最强版C#获取机器IP MAC信息*/string GetMacAddress()
        {
            string mac = "";
            try
            {
                //获取网卡硬件地址
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        mac = mo["MacAddress"].ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return mac;
            }
            catch
            {
                try
                {
                    System.Diagnostics.Process p = new System.Diagnostics.Process();
                    p.StartInfo.CreateNoWindow = true;
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.FileName = "nbtstat";
                    p.StartInfo.Arguments = " -A " + GetIP();
                    p.Start();
                    p.WaitForExit();
                    string s = p.StandardOutput.ReadToEnd();
                    if (Regex.IsMatch(s, @"MAC\s*Address\s*=\s*(([0-9a-zA-Z]{2}-)*[0-9a-zA-Z]{2})", RegexOptions.IgnoreCase))
                    {
                        mac = Regex.Match(s, @"MAC\s*Address\s*=\s*(([0-9a-zA-Z]{2}-)*[0-9a-zA-Z]{2})", RegexOptions.IgnoreCase).Groups[1].Value;
                    }
                    //mac = s.Substring(s.IndexOf("Physical Address. . . . . . . . . :") + 36, 17);
                    return mac;
                }
                catch
                {
                    try
                    {
                        System.Diagnostics.Process p = new System.Diagnostics.Process();
                        p.StartInfo.CreateNoWindow = true;
                        p.StartInfo.UseShellExecute = false;
                        p.StartInfo.RedirectStandardOutput = true;
                        p.StartInfo.FileName = "ipconfig";
                        p.StartInfo.Arguments = "/all";
                        p.Start();
                        p.WaitForExit();
                        string s = p.StandardOutput.ReadToEnd();
                        if (Regex.IsMatch(s, @"(([0-9a-zA-Z]{2}-)*[0-9a-zA-Z]{2})\r\r\n\s*DHCP\s*Enabled\s*(\s*.\s*)*", RegexOptions.IgnoreCase))
                        {
                            mac = Regex.Match(s, @"(([0-9a-zA-Z]{2}-)*[0-9a-zA-Z]{2})\r\r\n\s*DHCP\s*Enabled\s*(\s*.\s*)*", RegexOptions.IgnoreCase).Groups[1].Value;
                        }
                        //mac = s.Substring(s.IndexOf("Physical Address. . . . . . . . . :") + 36, 17);
                        return mac;
                    }
                    catch
                    {
                        return "unknow";
                    }
                }
                //return "unknow";  
            }
            finally
            {
            }

        }

        public static string GetIP()
        {
            string s = "";
            IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
            for (int i = 0; i < addressList.Length; i++)
            {
                s += addressList[i].ToString();
            }
            return s;
        }


评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.