请选择 进入手机版 | 继续访问电脑版

[C#教程] C# ManagementObjectSearcher操作window案例详解

[复制链接]
查看173 | 回复31 | 2021-9-12 12:52:08 | 显示全部楼层 |阅读模式

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

C# ManagementObjectSearcher操作window案例详解

前言 : 

我们在很多环境 下想要获得计算机的硬件或操作体系 的信息,比如获得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. //获取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. Win32_Processor, // CPU 处理器
  4. Win32_PhysicalMemory, // 物理内存条
  5. Win32_Keyboard, // 键盘
  6. Win32_PointingDevice, // 点输入设备,包括鼠标。
  7. Win32_FloppyDrive, // 软盘驱动器
  8. Win32_DiskDrive, // 硬盘驱动器
  9. Win32_CDROMDrive, // 光盘驱动器
  10. Win32_BaseBoard, // 主板
  11. Win32_BIOS, // BIOS 芯片
  12. Win32_ParallelPort, // 并口
  13. Win32_SerialPort, // 串口
  14. Win32_SerialPortConfiguration, // 串口配置
  15. Win32_SoundDevice, // 多媒体设置,一般指声卡。
  16. Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP)
  17. Win32_USBController, // USB 控制器
  18. Win32_NetworkAdapter, // 网络适配器
  19. Win32_NetworkAdapterConfiguration, // 网络适配器设置
  20. Win32_Printer, // 打印机
  21. Win32_PrinterConfiguration, // 打印机设置
  22. Win32_PrintJob, // 打印机任务
  23. Win32_TCPIPPrinterPort, // 打印机端口
  24. Win32_POTSModem, // MODEM
  25. Win32_POTSModemToSerialPort, // MODEM 端口
  26. Win32_DesktopMonitor, // 显示器
  27. Win32_DisplayConfiguration, // 显卡
  28. Win32_DisplayControllerConfiguration, // 显卡设置
  29. Win32_VideoController, // 显卡细节。
  30. Win32_VideoSettings, // 显卡支持的显示模式。
  31. // 操作系统
  32. Win32_TimeZone, // 时区
  33. Win32_SystemDriver, // 驱动程序
  34. Win32_DiskPartition, // 磁盘分区
  35. Win32_LogicalDisk, // 逻辑磁盘
  36. Win32_LogicalDiskToPartition, // 逻辑磁盘所在分区及始末位置。
  37. Win32_LogicalMemoryConfiguration, // 逻辑内存配置
  38. Win32_PageFile, // 系统页文件信息
  39. Win32_PageFileSetting, // 页文件设置
  40. Win32_BootConfiguration, // 系统启动配置
  41. Win32_ComputerSystem, // 计算机信息简要
  42. Win32_OperatingSystem, // 操作系统信息
  43. Win32_StartupCommand, // 系统自动启动程序
  44. Win32_Service, // 系统安装的服务
  45. Win32_Group, // 系统管理组
  46. Win32_GroupUser, // 系统组帐号
  47. Win32_UserAccount, // 用户帐号
  48. Win32_Process, // 系统进程
  49. Win32_Thread, // 系统线程
  50. Win32_Share, // 共享
  51. Win32_NetworkClient, // 已安装的网络客户端
  52. 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# ManagementObjectSearcher操作window案例详解的文章就先容 到这了,更多干系 C# ManagementObjectSearcher操作window内容请搜刮 脚本之家从前 的文章或继续欣赏 下面的干系 文章盼望 大家以后多多支持脚本之家!


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

avatar 老鼠飞出亚洲言 | 2021-9-14 16:00:16 | 显示全部楼层
管它三七二十一!
回复

使用道具 举报

avatar 快乐人L | 2021-9-14 22:52:44 | 显示全部楼层
这么版块的帖子越来越有深度了!
回复

使用道具 举报

avatar 我是的十八簿 | 2021-9-19 12:20:57 | 显示全部楼层
收藏了,admin楼主加油!
回复

使用道具 举报

avatar xj844662017 | 2021-9-21 08:43:59 | 显示全部楼层
我只看看不说话。。。
回复

使用道具 举报

avatar 形腿望舞 | 2021-9-21 16:03:58 | 显示全部楼层
楼上的心情不错啊!
回复

使用道具 举报

avatar 李亚兵 | 2021-9-22 04:38:44 | 显示全部楼层
admin楼主你想太多了!
回复

使用道具 举报

avatar 核桃脆 | 2021-9-23 05:26:11 | 显示全部楼层
顶!顶!顶!
回复

使用道具 举报

avatar 公路美学 | 2021-9-26 21:58:38 | 显示全部楼层
楼上的说的很好!
回复

使用道具 举报

avatar 123457683 | 2021-10-8 01:39:01 | 显示全部楼层
论坛的人气越来越旺了!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则