C#如何获取机器唯一标识符

C#如何获取机器唯一标识符,第1张

using System;

using SystemCollectionsGeneric;

using SystemComponentModel;

using SystemData;

using SystemDrawing;

using SystemText;

using SystemWindowsForms;

using SystemCollections;

using SystemManagement; 

 

namespace Test

{

    /// <summary>

    /// Computer Information

    /// </summary>

    public class Computer

    {

        public string CpuID;

        public string MacAddress;

        public string DiskID;

        public string IpAddress;

        public string LoginUserName;

        public string ComputerName;

        public string SystemType;

        public string TotalPhysicalMemory; //单位:M

        private static Computer _instance;

        public static Computer Instance()

        {

            if (_instance == null)

                _instance = new Computer();

            return _instance;

        }

        protected  Computer()

        {

            CpuID=GetCpuID();

            MacAddress=GetMacAddress();

            DiskID=GetDiskID();

            IpAddress=GetIPAddress();

            LoginUserName=GetUserName();

            SystemType=GetSystemType();

            TotalPhysicalMemory=GetTotalPhysicalMemory();

            ComputerName=GetComputerName();

        }

        string  GetCpuID()

        {

            try

            {

                //获取CPU序列号代码

                string cpuInfo = "";//cpu序列号

                ManagementClass mc = new ManagementClass("Win32_Processor");

                ManagementObjectCollection moc = mcGetInstances();

                foreach(ManagementObject mo in moc)

                {

                    cpuInfo = moProperties["ProcessorId"]ValueToString();

                }

                moc=null;

                mc=null;

                return cpuInfo;

            }

            catch

            {

                return "unknow";

            }

            finally

            {

            }

             

        }

        string  GetMacAddress()

        {

            try

            {

                //获取网卡硬件地址

                string mac="";

                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");

                ManagementObjectCollection moc = mcGetInstances();

                foreach(ManagementObject mo in moc)

                {

                    if((bool)mo["IPEnabled"] == true)

                    {

                        mac=mo["MacAddress"]ToString();

                        break;

                    }

                }

                moc=null;

                mc=null;

                return mac;

            }

            catch

            {

                return "unknow";

            }

            finally

            {

            }

             

        }

        string  GetIPAddress()

        {

            try

            {

                //获取IP地址

                string st="";

                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");

                ManagementObjectCollection moc = mcGetInstances();

                foreach(ManagementObject mo in moc)

                {

                    if((bool)mo["IPEnabled"] == true)

                    {

                        //st=mo["IpAddress"]ToString();

                        SystemArray ar;

                        ar=(SystemArray)(moProperties["IpAddress"]Value);

                        st=arGetValue(0)ToString();

                        break;

                    }

                }

                moc=null;

                mc=null;

                return st;

            }

            catch

            {

                return "unknow";

            }

            finally

            {

            }

             

        }

          

        string  GetDiskID()

        {

            try

            {

                //获取硬盘ID

                String HDid="";

                ManagementClass mc = new ManagementClass("Win32_DiskDrive");

                ManagementObjectCollection moc = mcGetInstances();

                foreach(ManagementObject mo in moc)

                {

                    HDid = (string)moProperties["Model"]Value;

                }

                moc=null;

                mc=null;

                return HDid;

            }

            catch

            {

                return "unknow";

            }

            finally

            {

            }

                 

        }

 

        /// <summary>

        ///  *** 作系统的登录用户名

        /// </summary>

        /// <returns></returns>

        string  GetUserName()

        {

            try

            {

                string st="";

                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");

                ManagementObjectCollection moc = mcGetInstances();

                foreach(ManagementObject mo in moc)

                {

                     

                    st=mo["UserName"]ToString();

                     

                }

                moc=null;

                mc=null;

                return st;

            }

            catch

            {

                return "unknow";

            }

            finally

            {

            }

             

        }

 

 

        /// <summary>

        /// PC类型

        /// </summary>

        /// <returns></returns>

        string  GetSystemType()

        {

            try

            {

                string st="";

                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");

                ManagementObjectCollection moc = mcGetInstances();

                foreach(ManagementObject mo in moc)

                {

                     

                    st=mo["SystemType"]ToString();

                     

                }

                moc=null;

                mc=null;

                return st;

            }

            catch

            {

                return "unknow";

            }

            finally

            {

            }

             

        }

 

        /// <summary>

        /// 物理内存

        /// </summary>

        /// <returns></returns>

        string  GetTotalPhysicalMemory()

        {

            try

            {

                 

                string st="";

                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");

                ManagementObjectCollection moc = mcGetInstances();

                foreach(ManagementObject mo in moc)

                {

                     

                    st=mo["TotalPhysicalMemory"]ToString();

                     

                }

                moc=null;

                mc=null;

                return st;

            }

            catch

            {

                return "unknow";

            }

            finally

            {

            }

        }

        /// <summary>

        /// 

        /// </summary>

        /// <returns></returns>

        string  GetComputerName()

        {

            try

            {                

                return SystemEnvironmentGetEnvironmentVariable("ComputerName");

            }

            catch

            {

                return "unknow";

            }

            finally

            {

            }

        }

 

 

    }

}

MAC地址,也就是网卡,对于单网卡来说是唯一的(64位)十六进值的物理地址,它是在出前厂就已经分配好了的.是不能在改动的。 关于MAC地址,为物理地址,是不可以改的,但可以用软件做地址欺骗。其实很多硬件有ID号的,如主板\CPU\内存\硬盘等。但大部分都检测不到。 判断计算机唯一标识,一般都用主板的ID号。几乎所有硬件检测软件都可以检测得到

         ///   <summary>    

         ///   获取网卡硬件地址    

         ///   </summary>    

        ///   <returns> string </returns>  

        //using SystemManagement;  

        public string GetMoAddress()   

         {   

             string MoAddress = " ";   

            using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))   

            {   

               ManagementObjectCollection moc2 = mcGetInstances();   

                 foreach (ManagementObject mo in moc2)   

               {   

                     if ((bool)mo["IPEnabled"] == true)   

                        MoAddress = mo["MacAddress"]ToString();   

                    moDispose();   

                }   

            }   

             return MoAddressToString();   

        }

 1,获取CPU编号

SystemManagementManagementClass mc = new ManagementClass("win32_processor"); 

ManagementObjectCollection moc = mcGetInstances(); 

foreach (ManagementObject mo in moc) 

    MessageBoxShow(mo["processorid"]ToString()); 

2,获取主板ID

SystemManagementManagementObjectSearcher searcher = new SystemManagementManagementObjectSearcher("SELECT  FROM Win32_PhysicalMedia");

 foreach (ManagementObject mo in searcherGet())

 {

       MessageBoxShow( mo["SerialNumber"]ToString()Trim());

}

或者:

SystemManagementManagementClass mc = new SystemManagementManagementClass("Win32_BaseBoard");

ManagementObjectCollection moc = mcGetInstances();

foreach (ManagementObject mx in moc)

 {

     MessageBoxShow( mxProperties["SerialNumber"]ValueToString());

}

3,获取硬盘ID

SystemManagementManagementClass mc = new SystemManagementManagementClass("Win32_DiskDrive");

 ManagementObjectCollection moc = mcGetInstances();

foreach (ManagementObject mo in moc)

{

     MessageBoxShow(moProperties["Model"]ValueToString());

}

4,获取网卡地址

 

SystemManagementManagementClass mc = new SystemManagementManagementClass("Win32_NetworkAdapterConfiguration");

 ManagementObjectCollection moc = mcGetInstances();

foreach (ManagementObject mo in moc)

{

     if((bool)mo["IPEnabled"] == true)

     MessageBoxShow(mo["MacAddress"]ToString());

     moDispose();

}

5,获取内存信息

SystemManagementManagementClass mcMemory = new SystemManagementManagementClass("Win32_OperatingSystem");

ManagementObjectCollection mocMemory = mcMemoryGetInstances();

foreach (ManagementObject mo in mocMemory)

 

{

if (moProperties["TotalVisibleMemorySize"]Value != null)

    {

        MessageBoxShow(moProperties["TotalVisibleMemorySize"]ValueToString());

      }

}

以上就是关于C#如何获取机器唯一标识符全部的内容,包括:C#如何获取机器唯一标识符、什么是计算机的唯一标识、怎么用WPF编程获取电脑唯一标识号,求详细,求简单代码。能用就好,初学者能看懂就好 不需要太复杂的。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9576379.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存