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 }