C#对window 硬件类操作,ManagementObjectSearcher

 

原文转载:http://blog.csdn.net/da_keng/article/details/50589145

纯属转载,复制过来方便编程时寻找。感谢作者:I-Awakening

 

复制前补充:

在刚学C#,用ManagementObjectSearcher 竟然不能解析到头文件,需要手动 Add Referance..

 

前言: 
我们在很多情况下想要获得计算机的硬件或操作系统的信息,比如获得CPU序列号给自己的软件添加机器码锁绑定指定电脑。又或者想要获得硬盘分区,声卡显卡等信息。

开篇:

我们用到的主要类是ManagementObjectSearcher,该类在System.Management命名空间下。 
有时候我们可以通过Environment获得一些简单的系统信息。 
如:Environment.MachineName;获得计算机名。 
Environment.UserName;获得操作系统登录用户名。 
不过在这篇文章中主要讨论ManagementObjectSearcher获取计算机硬件及操作系统的信息。

用法步骤:

  1. 添加引用:System.Management
  2. 引入命名空间:using System.Management;
  3. 创建ManagementObjectSearcher对象 
    anagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + Key); 
    其中的key见下面key列表:
  4. 通过searcher.Get()获得ManagementObjectCollection集合
  5. 遍历ManagementObjectCollection集合获得ManagementObject
  6. 通过managementObject[name]ManagementObject.GetPropertyValue(name)获得想要的属性 
    若不知道这里的name该写什么可以遍历打印一下:
 
  1. foreach (var property in managementObject.Properties)

  2. {

  3. Console.WriteLine(property.Name+":"+property.Value);

  4. }

  • 1
  • 2
  • 3
  • 4

示例:

 
  1. //获取CPU序列号

  2. public string GetCPUSerialNumber()

  3. {

  4. try

  5. {

  6. ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Processor");

  7. string sCPUSerialNumber = "";

  8. foreach (ManagementObject mo in searcher.Get())

  9. {

  10. sCPUSerialNumber = mo["ProcessorId"].ToString().Trim();

  11. break;

  12. }

  13. return sCPUSerialNumber;

  14. }

  15. catch

  16. {

  17. return "";

  18. }

  19. }

 
  1. /获取主板序列号

  2. public string GetBIOSSerialNumber()

  3. {

  4. try

  5. {

  6. ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_BIOS");

  7. string sBIOSSerialNumber = "";

  8. foreach (ManagementObject mo in searcher.Get())

  9. {

  10. sBIOSSerialNumber = mo.GetPropertyValue("SerialNumber").ToString().Trim();

  11. break;

  12. }

  13. return sBIOSSerialNumber;

  14. }

  15. catch

  16. {

  17. return "";

  18. }

  19. }

 
  1. //获取硬盘序列号

  2. public string GetHardDiskSerialNumber()

  3. {

  4. try

  5. {

  6. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

  7. string sHardDiskSerialNumber = "";

  8. foreach (ManagementObject mo in searcher.Get())

  9. {

  10. sHardDiskSerialNumber = mo["SerialNumber"].ToString().Trim();

  11. break;

  12. }

  13. return sHardDiskSerialNumber;

  14. }

  15. catch

  16. {

  17. return "";

  18. }

  19. }

 
  1. //获取网卡地址

  2. public string GetNetCardMACAddress()

  3. {

  4. try

  5. {

  6. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL) AND (Manufacturer <> 'Microsoft'))");

  7. string NetCardMACAddress = "";

  8. foreach (ManagementObject mo in searcher.Get())

  9. {

  10. NetCardMACAddress = mo["MACAddress"].ToString().Trim();

  11. break;

  12. }

  13. return NetCardMACAddress;

  14. }

  15. catch

  16. {

  17. return "";

  18. }

  19. }

常用Key值:

 
  1. ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + Key);

  2.  
  3. // 硬件

  4. Win32_Processor, // CPU 处理器

  5. Win32_PhysicalMemory, // 物理内存条

  6. Win32_Keyboard, // 键盘

  7. Win32_PointingDevice, // 点输入设备,包括鼠标。

  8. Win32_FloppyDrive, // 软盘驱动器

  9. Win32_DiskDrive, // 硬盘驱动器

  10. Win32_CDROMDrive, // 光盘驱动器

  11. Win32_BaseBoard, // 主板

  12. Win32_BIOS, // BIOS 芯片

  13. Win32_ParallelPort, // 并口

  14. Win32_SerialPort, // 串口

  15. Win32_SerialPortConfiguration, // 串口配置

  16. Win32_SoundDevice, // 多媒体设置,一般指声卡。

  17. Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP)

  18. Win32_USBController, // USB 控制器

  19. Win32_NetworkAdapter, // 网络适配器

  20. Win32_NetworkAdapterConfiguration, // 网络适配器设置

  21. Win32_Printer, // 打印机

  22. Win32_PrinterConfiguration, // 打印机设置

  23. Win32_PrintJob, // 打印机任务

  24. Win32_TCPIPPrinterPort, // 打印机端口

  25. Win32_POTSModem, // MODEM

  26. Win32_POTSModemToSerialPort, // MODEM 端口

  27. Win32_DesktopMonitor, // 显示器

  28. Win32_DisplayConfiguration, // 显卡

  29. Win32_DisplayControllerConfiguration, // 显卡设置

  30. Win32_VideoController, // 显卡细节。

  31. Win32_VideoSettings, // 显卡支持的显示模式。

  32.  
  33. // 操作系统

  34. Win32_TimeZone, // 时区

  35. Win32_SystemDriver, // 驱动程序

  36. Win32_DiskPartition, // 磁盘分区

  37. Win32_LogicalDisk, // 逻辑磁盘

  38. Win32_LogicalDiskToPartition, // 逻辑磁盘所在分区及始末位置。

  39. Win32_LogicalMemoryConfiguration, // 逻辑内存配置

  40. Win32_PageFile, // 系统页文件信息

  41. Win32_PageFileSetting, // 页文件设置

  42. Win32_BootConfiguration, // 系统启动配置

  43. Win32_ComputerSystem, // 计算机信息简要

  44. Win32_OperatingSystem, // 操作系统信息

  45. Win32_StartupCommand, // 系统自动启动程序

  46. Win32_Service, // 系统安装的服务

  47. Win32_Group, // 系统管理组

  48. Win32_GroupUser, // 系统组帐号

  49. Win32_UserAccount, // 用户帐号

  50. Win32_Process, // 系统进程

  51. Win32_Thread, // 系统线程

  52. Win32_Share, // 共享

  53. Win32_NetworkClient, // 已安装的网络客户端

  54. Win32_NetworkProtocol, // 已安装的网络协议

所有Key:

 
  1. Win32_1394Controller

  2. Win32_1394ControllerDevice

  3. Win32_Account

  4. Win32_AccountSID

  5. Win32_ACE

  6. Win32_ActionCheck

  7. Win32_AllocatedResource

  8. Win32_ApplicationCommandLine

  9. Win32_ApplicationService

  10. Win32_AssociatedBattery

  11. Win32_AssociatedProcessorMemory

  12. Win32_BaseBoard

  13. Win32_BaseService

  14. Win32_Battery

  15. Win32_Binary

  16. Win32_BindImageAction

  17. Win32_BIOS

  18. Win32_BootConfiguration

  19. Win32_Bus

  20. Win32_CacheMemory

  21. Win32_CDROMDrive

  22. Win32_CheckCheck

  23. Win32_CIMLogicalDeviceCIMDataFile

  24. Win32_ClassicCOMApplicationClasses

  25. Win32_ClassicCOMClass

  26. Win32_ClassicCOMClassSetting

  27. Win32_ClassicCOMClassSettings

  28. Win32_ClassInfoAction

  29. Win32_ClientApplicationSetting

  30. Win32_CodecFile

  31. Win32_COMApplication

  32. Win32_COMApplicationClasses

  33. Win32_COMApplicationSettings

  34. Win32_COMClass

  35. Win32_ComClassAutoEmulator

  36. Win32_ComClassEmulator

  37. Win32_CommandLineAccess

  38. Win32_ComponentCategory

  39. Win32_ComputerSystem

  40. Win32_ComputerSystemProcessor

  41. Win32_ComputerSystemProduct

  42. Win32_COMSetting

  43. Win32_Condition

  44. Win32_CreateFolderAction

  45. Win32_CurrentProbe

  46. Win32_DCOMApplication

  47. Win32_DCOMApplicationAccessAllowedSetting

  48. Win32_DCOMApplicationLaunchAllowedSetting

  49. Win32_DCOMApplicationSetting

  50. Win32_DependentService

  51. Win32_Desktop

  52. Win32_DesktopMonitor

  53. Win32_DeviceBus

  54. Win32_DeviceMemoryAddress

  55. Win32_DeviceSettings

  56. Win32_Directory

  57. Win32_DirectorySpecification

  58. Win32_DiskDrive

  59. Win32_DiskDriveToDiskPartition

  60. Win32_DiskPartition

  61. Win32_DisplayConfiguration

  62. Win32_DisplayControllerConfiguration

  63. Win32_DMAChannel

  64. Win32_DriverVXD

  65. Win32_DuplicateFileAction

  66. Win32_Environment

  67. Win32_EnvironmentSpecification

  68. Win32_ExtensionInfoAction

  69. Win32_Fan

  70. Win32_FileSpecification

  71. Win32_FloppyController

  72. Win32_FloppyDrive

  73. Win32_FontInfoAction

  74. Win32_Group

  75. Win32_GroupUser

  76. Win32_HeatPipe

  77. Win32_IDEController

  78. Win32_IDEControllerDevice

  79. Win32_ImplementedCategory

  80. Win32_InfraredDevice

  81. Win32_IniFileSpecification

  82. Win32_InstalledSoftwareElement

  83. Win32_IRQResource

  84. Win32_Keyboard

  85. Win32_LaunchCondition

  86. Win32_LoadOrderGroup

  87. Win32_LoadOrderGroupServiceDependencies

  88. Win32_LoadOrderGroupServiceMembers

  89. Win32_LogicalDisk

  90. Win32_LogicalDiskRootDirectory

  91. Win32_LogicalDiskToPartition

  92. Win32_LogicalFileAccess

  93. Win32_LogicalFileAuditing

  94. Win32_LogicalFileGroup

  95. Win32_LogicalFileOwner

  96. Win32_LogicalFileSecuritySetting

  97. Win32_LogicalMemoryConfiguration

  98. Win32_LogicalProgramGroup

  99. Win32_LogicalProgramGroupDirectory

  100. Win32_LogicalProgramGroupItem

  101. Win32_LogicalProgramGroupItemDataFile

  102. Win32_LogicalShareAccess

  103. Win32_LogicalShareAuditing

  104. Win32_LogicalShareSecuritySetting

  105. Win32_ManagedSystemElementResource

  106. Win32_MemoryArray

  107. Win32_MemoryArrayLocation

  108. Win32_MemoryDevice

  109. Win32_MemoryDeviceArray

  110. Win32_MemoryDeviceLocation

  111. Win32_MethodParameterClass

  112. Win32_MIMEInfoAction

  113. Win32_MotherboardDevice

  114. Win32_MoveFileAction

  115. Win32_MSIResource

  116. Win32_networkAdapter

  117. Win32_networkAdapterConfiguration

  118. Win32_networkAdapterSetting

  119. Win32_networkClient

  120. Win32_networkConnection

  121. Win32_networkLoginProfile

  122. Win32_networkProtocol

  123. Win32_NTEventlogFile

  124. Win32_NTLogEvent

  125. Win32_NTLogEventComputer

  126. Win32_NTLogEventLog

  127. Win32_NTLogEventUser

  128. Win32_ODBCAttribute

  129. Win32_ODBCDataSourceAttribute

  130. Win32_ODBCDataSourceSpecification

  131. Win32_ODBCDriverAttribute

  132. Win32_ODBCDriverSoftwareElement

  133. Win32_ODBCDriverSpecification

  134. Win32_ODBCSourceAttribute

  135. Win32_ODBCTranslatorSpecification

  136. Win32_OnBoardDevice

  137. Win32_OperatingSystem

  138. Win32_OperatingSystemQFE

  139. Win32_OSRecoveryConfiguration

  140. Win32_PageFile

  141. Win32_PageFileElementSetting

  142. Win32_PageFileSetting

  143. Win32_PageFileUsage

  144. Win32_ParallelPort

  145. Win32_Patch

  146. Win32_PatchFile

  147. Win32_PatchPackage

  148. Win32_PCMCIAController

  149. Win32_Perf

  150. Win32_PerfRawData

  151. Win32_PerfRawData_ASP_ActiveServerPages

  152. Win32_PerfRawData_ASPnet_114322_ASPnetAppsv114322

  153. Win32_PerfRawData_ASPnet_114322_ASPnetv114322

  154. Win32_PerfRawData_ASPnet_ASPnet

  155. Win32_PerfRawData_ASPnet_ASPnetApplications

  156. Win32_PerfRawData_IAS_IASAccountingClients

  157. Win32_PerfRawData_IAS_IASAccountingServer

  158. Win32_PerfRawData_IAS_IASAuthenticationClients

  159. Win32_PerfRawData_IAS_IASAuthenticationServer

  160. Win32_PerfRawData_InetInfo_InternetInformationServicesGlobal

  161. Win32_PerfRawData_MSDTC_DistributedTransactionCoordinator

  162. Win32_PerfRawData_MSFTPSVC_FTPService

  163. Win32_PerfRawData_MSSQLSERVER_SQLServerAccessMethods

  164. Win32_PerfRawData_MSSQLSERVER_SQLServerBackupDevice

  165. Win32_PerfRawData_MSSQLSERVER_SQLServerBufferManager

  166. Win32_PerfRawData_MSSQLSERVER_SQLServerBufferPartition

  167. Win32_PerfRawData_MSSQLSERVER_SQLServerCacheManager

  168. Win32_PerfRawData_MSSQLSERVER_SQLServerDatabases

  169. Win32_PerfRawData_MSSQLSERVER_SQLServerGeneralStatistics

  170. Win32_PerfRawData_MSSQLSERVER_SQLServerLatches

  171. Win32_PerfRawData_MSSQLSERVER_SQLServerLocks

  172. Win32_PerfRawData_MSSQLSERVER_SQLServerMemoryManager

  173. Win32_PerfRawData_MSSQLSERVER_SQLServerReplicationAgents

  174. Win32_PerfRawData_MSSQLSERVER_SQLServerReplicationDist

  175. Win32_PerfRawData_MSSQLSERVER_SQLServerReplicationLogreader

  176. Win32_PerfRawData_MSSQLSERVER_SQLServerReplicationMerge

  177. Win32_PerfRawData_MSSQLSERVER_SQLServerReplicationSnapshot

  178. Win32_PerfRawData_MSSQLSERVER_SQLServerSQLStatistics

  179. Win32_PerfRawData_MSSQLSERVER_SQLServerUserSettable

  180. Win32_PerfRawData_netFramework_netCLRExceptions

  181. Win32_PerfRawData_netFramework_netCLRInterop

  182. Win32_PerfRawData_netFramework_netCLRJit

  183. Win32_PerfRawData_netFramework_netCLRLoading

  184. Win32_PerfRawData_netFramework_netCLRLocksAndThreads

  185. Win32_PerfRawData_netFramework_netCLRMemory

  186. Win32_PerfRawData_netFramework_netCLRRemoting

  187. Win32_PerfRawData_netFramework_netCLRSecurity

  188. Win32_PerfRawData_Outlook_Outlook

  189. Win32_PerfRawData_PerfDisk_PhysicalDisk

  190. Win32_PerfRawData_Perfnet_Browser

  191. Win32_PerfRawData_Perfnet_Redirector

  192. Win32_PerfRawData_Perfnet_Server

  193. Win32_PerfRawData_Perfnet_ServerWorkQueues

  194. Win32_PerfRawData_PerfOS_Cache

  195. Win32_PerfRawData_PerfOS_Memory

  196. Win32_PerfRawData_PerfOS_Objects

  197. Win32_PerfRawData_PerfOS_PagingFile

  198. Win32_PerfRawData_PerfOS_Processor

  199. Win32_PerfRawData_PerfOS_System

  200. Win32_PerfRawData_PerfProc_FullImage_Costly

  201. Win32_PerfRawData_PerfProc_Image_Costly

  202. Win32_PerfRawData_PerfProc_JobObject

  203. Win32_PerfRawData_PerfProc_JobObjectDetails

  204. Win32_PerfRawData_PerfProc_Process

  205. Win32_PerfRawData_PerfProc_ProcessAddressSpace_Costly

  206. Win32_PerfRawData_PerfProc_Thread

  207. Win32_PerfRawData_PerfProc_ThreadDetails_Costly

  208. Win32_PerfRawData_RemoteAccess_RASPort

  209. Win32_PerfRawData_RemoteAccess_RASTotal

  210. Win32_PerfRawData_RSVP_ACSPerRSVPService

  211. Win32_PerfRawData_Spooler_PrintQueue

  212. Win32_PerfRawData_TapiSrv_Telephony

  213. Win32_PerfRawData_Tcpip_ICMP

  214. Win32_PerfRawData_Tcpip_IP

  215. Win32_PerfRawData_Tcpip_NBTConnection

  216. Win32_PerfRawData_Tcpip_networkInterface

  217. Win32_PerfRawData_Tcpip_TCP

  218. Win32_PerfRawData_Tcpip_UDP

  219. Win32_PerfRawData_W3SVC_WebService

  220. Win32_PhysicalMedia

  221. Win32_PhysicalMemory

  222. Win32_PhysicalMemoryArray

  223. Win32_PhysicalMemoryLocation

  224. Win32_PNPAllocatedResource

  225. Win32_PnPDevice

  226. Win32_PnPEntity

  227. Win32_PointingDevice

  228. Win32_PortableBattery

  229. Win32_PortConnector

  230. Win32_PortResource

  231. Win32_POTSModem

  232. Win32_POTSModemToSerialPort

  233. Win32_PowerManagementEvent

  234. Win32_Printer

  235. Win32_PrinterConfiguration

  236. Win32_PrinterController

  237. Win32_PrinterDriverDll

  238. Win32_PrinterSetting

  239. Win32_PrinterShare

  240. Win32_PrintJob

  241. Win32_PrivilegesStatus

  242. Win32_Process

  243. Win32_Processor

  244. Win32_ProcessStartup

  245. Win32_Product

  246. Win32_ProductCheck

  247. Win32_ProductResource

  248. Win32_ProductSoftwareFeatures

  249. Win32_ProgIDSpecification

  250. Win32_ProgramGroup

  251. Win32_ProgramGroupContents

  252. Win32_ProgramGroupOrItem

  253. Win32_Property

  254. Win32_ProtocolBinding

  255. Win32_PublishComponentAction

  256. Win32_QuickFixEngineering

  257. Win32_Refrigeration

  258. Win32_Registry

  259. Win32_RegistryAction

  260. Win32_RemoveFileAction

  261. Win32_RemoveIniAction

  262. Win32_ReserveCost

  263. Win32_ScheduledJob

  264. Win32_SCSIController

  265. Win32_SCSIControllerDevice

  266. Win32_SecurityDescriptor

  267. Win32_SecuritySetting

  268. Win32_SecuritySettingAccess

  269. Win32_SecuritySettingAuditing

  270. Win32_SecuritySettingGroup

  271. Win32_SecuritySettingOfLogicalFile

  272. Win32_SecuritySettingOfLogicalShare

  273. Win32_SecuritySettingOfObject

  274. Win32_SecuritySettingOwner

  275. Win32_SelfRegModuleAction

  276. Win32_SerialPort

  277. Win32_SerialPortConfiguration

  278. Win32_SerialPortSetting

  279. Win32_Service

  280. Win32_ServiceControl

  281. Win32_ServiceSpecification

  282. Win32_ServiceSpecificationService

  283. Win32_SettingCheck

  284. Win32_Share

  285. Win32_ShareToDirectory

  286. Win32_ShortcutAction

  287. Win32_ShortcutFile

  288. Win32_ShortcutSAP

  289. Win32_SID

  290. Win32_SMBIOSMemory

  291. Win32_SoftwareElement

  292. Win32_SoftwareElementAction

  293. Win32_SoftwareElementCheck

  294. Win32_SoftwareElementCondition

  295. Win32_SoftwareElementResource

  296. Win32_SoftwareFeature

  297. Win32_SoftwareFeatureAction

  298. Win32_SoftwareFeatureCheck

  299. Win32_SoftwareFeatureParent

  300. Win32_SoftwareFeatureSoftwareElements

  301. Win32_SoundDevice

  302. Win32_StartupCommand

  303. Win32_SubDirectory

  304. Win32_SystemAccount

  305. Win32_SystemBIOS

  306. Win32_SystemBootConfiguration

  307. Win32_SystemDesktop

  308. Win32_SystemDevices

  309. Win32_SystemDriver

  310. Win32_SystemDriverPNPEntity

  311. Win32_SystemEnclosure

  312. Win32_SystemLoadOrderGroups

  313. Win32_SystemLogicalMemoryConfiguration

  314. Win32_SystemMemoryResource

  315. Win32_SystemnetworkConnections

  316. Win32_SystemOperatingSystem

  317. Win32_SystemPartitions

  318. Win32_SystemProcesses

  319. Win32_SystemProgramGroups

  320. Win32_SystemResources

  321. Win32_SystemServices

  322. Win32_SystemSetting

  323. Win32_SystemSlot

  324. Win32_SystemSystemDriver

  325. Win32_SystemTimeZone

  326. Win32_SystemUsers

  327. Win32_TapeDrive

  328. Win32_TemperatureProbe

  329. Win32_Thread

  330. Win32_TimeZone

  331. Win32_Trustee

  332. Win32_TypeLibraryAction

  333. Win32_UninterruptiblePowerSupply

  334. Win32_USBController

  335. Win32_USBControllerDevice

  336. Win32_UserAccount

  337. Win32_UserDesktop

  338. Win32_VideoConfiguration

  339. Win32_VideoController

  340. Win32_VideoSettings

  341. Win32_VoltageProbe

  342. Win32_WMIElementSetting

  343. Win32_WMISetting

你可能感兴趣的:(C#,窗体)