zoukankan      html  css  js  c++  java
  • c#获取硬件信息

      1 string GetCpuID()
      2         {
      3             try
      4             {
      5                 //获取CPU序列号代码 
      6                 string cpuInfo = "";//cpu序列号 
      7                 
      8                 ManagementClass mc = new ManagementClass("Win32_Processor");
      9                 ManagementObjectCollection moc = mc.GetInstances();
     10                 foreach (ManagementObject mo in moc)
     11                 {
     12                     cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
     13                 }
     14                 moc = null;
     15                 mc = null;
     16                 return "cpu序列号:"+cpuInfo;
     17             }
     18             catch
     19             {
     20                 return "unknow";
     21             }
     22             finally
     23             {
     24             }
     25         }
     26         string GetMacAddress()
     27         {
     28             try
     29             {
     30                 //获取网卡硬件地址 
     31                 string mac = "";
     32                 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
     33                 ManagementObjectCollection moc = mc.GetInstances();
     34                 foreach (ManagementObject mo in moc)
     35                 {
     36                     if ((bool)mo["IPEnabled"] == true)
     37                     {
     38                         mac = mo["MacAddress"].ToString();
     39                         break;
     40                     }
     41                 }
     42                 moc = null;
     43                 mc = null;
     44                 return "网卡MAC地址:"+mac;
     45             }
     46             catch
     47             {
     48                 return "unknow";
     49             }
     50             finally
     51             {
     52             }
     53         }
     54         string GetIPAddress()
     55         {
     56             try
     57             {
     58                 //获取IP地址 
     59                 string st = "";
     60                 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
     61                 ManagementObjectCollection moc = mc.GetInstances();
     62                 foreach (ManagementObject mo in moc)
     63                 {
     64                     if ((bool)mo["IPEnabled"] == true)
     65                     {
     66                         //st=mo["IpAddress"].ToString(); 
     67                         System.Array ar;
     68                         ar = (System.Array)(mo.Properties["IpAddress"].Value);
     69                         st = ar.GetValue(0).ToString();
     70                         break;
     71                     }
     72                 }
     73                 moc = null;
     74                 mc = null;
     75                 return "IP地址:"+st;
     76             }
     77             catch
     78             {
     79                 return "unknow";
     80             }
     81             finally
     82             {
     83             }
     84         }
     85         string GetDiskID()
     86         {
     87             try
     88             {
     89                 //获取硬盘ID 
     90                 String HDid = "";
     91                 ManagementClass mc = new ManagementClass("Win32_DiskDrive");
     92                 ManagementObjectCollection moc = mc.GetInstances();
     93                 foreach (ManagementObject mo in moc)
     94                 {
     95                     HDid = (string)mo.Properties["Model"].Value;
     96                 }
     97                 moc = null;
     98                 mc = null;
     99                 return "硬盘序列号:"+HDid;
    100             }
    101             catch
    102             {
    103                 return "unknow";
    104             }
    105             finally
    106             {
    107             }
    108         }
    109         /// <summary> 
    110         /// 操作系统的登录用户名 
    111         /// </summary> 
    112         /// <returns></returns> 
    113         string GetUserName()
    114         {
    115             try
    116             {
    117                 string st = "";
    118                 ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
    119                 ManagementObjectCollection moc = mc.GetInstances();
    120                 foreach (ManagementObject mo in moc)
    121                 {
    122                     st = mo["UserName"].ToString();
    123                 }
    124                 moc = null;
    125                 mc = null;
    126                 return "系统登录用户名:"+st;
    127             }
    128             catch
    129             {
    130                 return "unknow";
    131             }
    132             finally
    133             {
    134             }
    135         }
    136         /// <summary> 
    137         /// PC类型 
    138         /// </summary> 
    139         /// <returns></returns> 
    140         string GetSystemType()
    141         {
    142             try
    143             {
    144                 string st = "";
    145                 ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
    146                 ManagementObjectCollection moc = mc.GetInstances();
    147                 foreach (ManagementObject mo in moc)
    148                 {
    149                     st = mo["SystemType"].ToString();
    150                 }
    151                 moc = null;
    152                 mc = null;
    153                 return "电脑类型:"+st;
    154             }
    155             catch
    156             {
    157                 return "unknow";
    158             }
    159             finally
    160             {
    161             }
    162         }
    163         /// <summary> 
    164         /// 物理内存 
    165         /// </summary> 
    166         /// <returns></returns> 
    167         string GetTotalPhysicalMemory()
    168         {
    169             try
    170             {
    171                 string st = "";
    172                 ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
    173                 ManagementObjectCollection moc = mc.GetInstances();
    174                 foreach (ManagementObject mo in moc)
    175                 {
    176                     st = mo["TotalPhysicalMemory"].ToString();
    177                 }
    178                 moc = null;
    179                 mc = null;
    180                 return "物理内存:"+st;
    181             }
    182             catch
    183             {
    184                 return "unknow";
    185             }
    186             finally
    187             {
    188             }
    189         }
    190         /// <summary> 
    191         ///  获取计算机名称
    192         /// </summary> 
    193         /// <returns></returns> 
    194         string GetComputerName()
    195         {
    196             try
    197             {
    198                 return "计算机名:"+System.Environment.GetEnvironmentVariable("ComputerName");
    199             }
    200             catch
    201             {
    202                 return "unknow";
    203             }
    204             finally
    205             {
    206             }
    207         }
  • 相关阅读:
    什么是ORM
    ORM优缺点
    Azure 中快速搭建 FTPS 服务
    连接到 Azure 上的 SQL Server 虚拟机(经典部署)
    在 Azure 虚拟机中配置 Always On 可用性组(经典)
    SQL Server 2014 虚拟机的自动备份 (Resource Manager)
    Azure 虚拟机上的 SQL Server 常见问题
    排查在 Azure 中新建 Windows 虚拟机时遇到的经典部署问题
    上传通用化 VHD 并使用它在 Azure 中创建新 VM
    排查在 Azure 中新建 Windows VM 时遇到的部署问题
  • 原文地址:https://www.cnblogs.com/hanc/p/3685942.html
Copyright © 2011-2022 走看看