.Net获取远程mac地址


        /// <summary>
        /// 获取mac地址
        /// </summary>
        /// <returns></returns>
        private string GetMac()
        {
            string MAC = "";
            ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection MOC = MC.GetInstances();
            foreach (ManagementObject moc in MOC)
            {
                if (moc["IPEnabled"].ToString() == "True")
                {
                    MAC = moc["MacAddress"].ToString();
                }
            }
            return MAC;
        }
        public string GetCustomerMac(string IP) //para IP is the client's IP
        {
            if (IP == "127.0.0.1")
            {
                return GetMac();
            }//"00-04-61-5C-31-52";//00-0F-1F-C6-B2-B3
            else
            {
                string dirResults = "";
                ProcessStartInfo psi = new ProcessStartInfo();
                Process proc = new Process();
                psi.FileName = "nbtstat";
                psi.RedirectStandardInput = false;
                psi.RedirectStandardOutput = true;
                psi.Arguments = "-A " + IP;
                psi.UseShellExecute = false;
                proc = Process.Start(psi);
                dirResults = proc.StandardOutput.ReadToEnd();
                proc.WaitForExit();
                dirResults = dirResults.Replace("\r", "").Replace("\n", "").Replace("\t", "");
                int i = dirResults.LastIndexOf("=");
                dirResults = dirResults.Substring(i + 2, 17);
                if (dirResults.IndexOf("本地连接") != -1)
                { dirResults = "没有得到mac"; }
                return dirResults;
            }
        }

posted @ 2012-11-22 16:41  木子易  Views(1530)  Comments(0Edit  收藏  举报