• C#获取硬件信息


    1. 引用命名空间
       using System.Runtime.InteropServices;
       using System.Management;
     
    2. 取机器名
    public string GetHostName()
    {
        return System.Net.Dns.GetHostName();
    }
     
    3. 取CPU编号
    public String GetCpuID()
    {
      try
      {
          ManagementClass mc = new ManagementClass("Win32_Processor");
          ManagementObjectCollection moc = mc.GetInstances()
          String strCpuID = string.Empty;
          foreach (ManagementObject mo in moc)
          {
             strCpuID = mo.Properties["ProcessorId"].Value.ToString();
             break;
          }
          return strCpuID;
       }
       catch(Exception er)
       {
           return "";
       }
    }
     
     
    4. 取第一块硬盘编号
    public String GetHardDiskID()
    {
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
            String strHardDiskID = string.Empty;
            foreach (ManagementObject mo in searcher.Get())
            {
                strHardDiskID = mo["SerialNumber"].ToString().Trim();
                break;
            }
            return strHardDiskID;
         }
         catch(Exception er)
         {
            //handle exception
            return "";
         }
    }
     
     
    5. 以下是取得Mac地址
        [StructLayout(LayoutKind.Sequential)]
            public struct NAME_BUFFER
            {
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]
                public byte[] name;
                public byte name_num;
                public byte name_flags;
            }
     
            [StructLayout(LayoutKind.Sequential)]
            public struct NCB
            {
                public byte ncb_command;
                public byte ncb_retcode;
                public byte ncb_lsn;
                public byte ncb_num;
                public IntPtr ncb_buffer;
                public ushort ncb_length;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]
                public byte[] ncb_callname;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]
                public byte[] ncb_name;
                public byte ncb_rto;
                public byte ncb_sto;
                public IntPtr ncb_post;
                public byte ncb_lana_num;
                public byte ncb_cmd_cplt;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
                public byte[] ncb_reserve;
                public IntPtr ncb_event;
            }
     
            [StructLayout(LayoutKind.Sequential)]
            public struct LANA_ENUM
            {
                public byte length;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.MAX_LANA)]
                public byte[] lana;
            }
     
            [StructLayout(LayoutKind.Auto)]
            public struct ASTAT
            {
                public ADAPTER_STATUS adapt;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NUM_NAMEBUF)]
                public NAME_BUFFER[] NameBuff;
            }
            public class Win32API
            {
                [DllImport("NETAPI32.DLL")]
                public static extern char Netbios(ref NCB ncb);
            }
     
            public string GetMacAddress()
            {
                string addr = "";
                try
                {
                    int cb;
                    ASTAT adapter;
                    NCB Ncb = new NCB();
                    char uRetCode;
                    LANA_ENUM lenum;
     
                    Ncb.ncb_command = (byte)NCBCONST.NCBENUM;
                    cb = Marshal.SizeOf(typeof(LANA_ENUM));
                    Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
                    Ncb.ncb_length = (ushort)cb;
                    uRetCode = Win32API.Netbios(ref Ncb);
                    lenum = (LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(LANA_ENUM));
                    Marshal.FreeHGlobal(Ncb.ncb_buffer);
                    if (uRetCode != (short)NCBCONST.NRC_GOODRET)
                        return "";
     
                    for (int i = 0; i < lenum.length; i++)
                    {
                        Ncb.ncb_command = (byte)NCBCONST.NCBRESET;
                        Ncb.ncb_lana_num = lenum.lana[i];
                        uRetCode = Win32API.Netbios(ref Ncb);
                        if (uRetCode != (short)NCBCONST.NRC_GOODRET)
                            return "";
     
                        Ncb.ncb_command = (byte)NCBCONST.NCBASTAT;
                        Ncb.ncb_lana_num = lenum.lana[i];
                        Ncb.ncb_callname[0] = (byte)'*';
                        cb = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER)) * (int)NCBCONST.NUM_NAMEBUF;
                        Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
                        Ncb.ncb_length = (ushort)cb;
                        uRetCode = Win32API.Netbios(ref Ncb);
                        adapter.adapt = (ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(ADAPTER_STATUS));
                        Marshal.FreeHGlobal(Ncb.ncb_buffer);
     
                        if (uRetCode == (short)NCBCONST.NRC_GOODRET)
                        {
                            if (i > 0)
                                addr += ":";
                            addr = string.Format("{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}",
                             adapter.adapt.adapter_address[0],
                             adapter.adapt.adapter_address[1],
                             adapter.adapt.adapter_address[2],
                             adapter.adapt.adapter_address[3],
                             adapter.adapt.adapter_address[4],
                             adapter.adapt.adapter_address[5]);
                        }
                    }
                }
                catch(Exception ex)
                {
                    //handle exception                         
                }
                return addr.Replace(' ', '0');
            }
  • 相关阅读:
    关于postman与shiro权限验证问题
    springboot对shiro进行mock单元测试
    深入理解spring注解之@ComponentScan注解
    springboot项目启动,但是访问报404错误
    通过jedis连接redis单机成功,使用redis客户端可以连接集群,但使用JedisCluster连接redis集群一直报Could not get a resource from the pool
    重装系统后ORACLE数据库恢复的方法
    ORA-03113: end-of-file on communication channel 解决方法
    ORA-03113:通信通道的文件结尾-完美解决方案
    由于Windows和Linux行尾标识引起脚本无法运行的解决
    在cmd命令行中弹出Windows对话框(使用mshta.exe命令)
  • 原文地址:https://www.cnblogs.com/happinesshappy/p/4584151.html
Copyright © 2020-2023  润新知