Mac地址的读取(包括base64的转换)

 string mac =null;
        /// 
        /// 获取MAC地址
        /// 
        /// mac地址
        public  ReadMac()//获取本机MAC地址
        {
            
            ManagementClass mc;
            mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if (mo["IPEnabled"].ToString() == "True")
                    mac = mo["MacAddress"].ToString();
            }            
        }
        /// 
        /// mac地址转换成base64字符
        /// 
        /// 原始mac地址
        /// 转换后的mac地址
        public  string ChangeMac()
        {
            if (mac != null)
            {
                string str = mac.Replace(":", "");//去除mac中的":"
                var strMac = Encoding.Default.GetBytes(str);//对其中字符进行编码
                string endStr = Convert.ToBase64String(strMac);//将编码后的字符转换成base64
                //string endStr = Encoding.Default.GetString(strMac);
                return endStr;
            }
            else
            {
                return "Mac地址转换失败";
            }
        }
        /// 
        /// Mac base64后的解码
        /// 
        /// base64编码字符
        /// 原始去除“:”的Mac
        public  string firstMac()
        {
            //var baseStr = Convert.FromBase64String (str);
            //string firstStr = Encoding.Default.GetString(baseStr);
            if (mac != null)
            {
                string firstStr = mac.Replace(":", "");
                return firstStr;
            }
            else
            {
                return "本机Mac请求失败";
            }
        }
        /// 
        /// 检查Mac地址是否匹配
        /// 
        /// 服务器路径
        /// 
        public int CheckMac(string url)
        {
            string strCheckMac= PostUpload.PostMoth(url,"mac="+ ChangeMac() , Encoding.Default);//true上传成功,false上传失败
            if (strCheckMac == "true")
            {
                return 1;
            }
            else
            {
                return 0;
            }

        }
    }

你可能感兴趣的:(C#,asp.net)