欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# ManagementObjectSearcher操作window案例详解

程序员文章站 2022-06-15 18:39:00
在刚学c#,用managementobjectsearcher 竟然不能解析到头文件,需要手动 add referance..前言: 我们在很多情况下想要获得计算机的硬件或操作系统的信息,比...

在刚学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该写什么可以遍历打印一下:
foreach (var property in managementobject.properties)
{
    console.writeline(property.name+":"+property.value);
}

示例:

//获取cpu序列号
public string getcpuserialnumber()
{
    try
    {
        managementobjectsearcher searcher = new managementobjectsearcher("select * from win32_processor");
        string scpuserialnumber = "";
        foreach (managementobject mo in searcher.get())
        {
            scpuserialnumber = mo["processorid"].tostring().trim();
            break;
        }
        return scpuserialnumber;
    }
    catch
    {
        return "";
    }
}
/获取主板序列号
public string getbiosserialnumber()
{
    try
    {
        managementobjectsearcher searcher = new managementobjectsearcher("select * from win32_bios");
        string sbiosserialnumber = "";
        foreach (managementobject mo in searcher.get())
        {
            sbiosserialnumber = mo.getpropertyvalue("serialnumber").tostring().trim();
            break;
        }
        return sbiosserialnumber;
    }
    catch
    {
        return "";
    }
}
//获取硬盘序列号
public string getharddiskserialnumber()
{
    try
    {
        managementobjectsearcher searcher = new managementobjectsearcher("select * from win32_physicalmedia");
        string sharddiskserialnumber = "";
        foreach (managementobject mo in searcher.get())
        {
            sharddiskserialnumber = mo["serialnumber"].tostring().trim();
            break;
        }
        return sharddiskserialnumber;
    }
    catch
    {
        return "";
    }
}
//获取网卡地址
public string getnetcardmacaddress()
{
    try
    {
        managementobjectsearcher searcher = new managementobjectsearcher("select * from win32_networkadapter where ((macaddress is not null) and (manufacturer <> 'microsoft'))");
        string netcardmacaddress = "";
        foreach (managementobject mo in searcher.get())
        {
            netcardmacaddress = mo["macaddress"].tostring().trim();
            break;
        }
        return netcardmacaddress;
    }
    catch
    {
        return "";
    }
}

常用key值:

managementobjectsearcher searcher = new managementobjectsearcher("select * from " + key);
 
// 硬件 
win32_processor, // cpu 处理器 
win32_physicalmemory, // 物理内存条 
win32_keyboard, // 键盘 
win32_pointingdevice, // 点输入设备,包括鼠标。 
win32_floppydrive, // 软盘驱动器 
win32_diskdrive, // 硬盘驱动器 
win32_cdromdrive, // 光盘驱动器 
win32_baseboard, // 主板 
win32_bios, // bios 芯片 
win32_parallelport, // 并口 
win32_serialport, // 串口 
win32_serialportconfiguration, // 串口配置 
win32_sounddevice, // 多媒体设置,一般指声卡。 
win32_systemslot, // 主板插槽 (isa & pci & agp) 
win32_usbcontroller, // usb 控制器 
win32_networkadapter, // 网络适配器 
win32_networkadapterconfiguration, // 网络适配器设置 
win32_printer, // 打印机 
win32_printerconfiguration, // 打印机设置 
win32_printjob, // 打印机任务 
win32_tcpipprinterport, // 打印机端口 
win32_potsmodem, // modem 
win32_potsmodemtoserialport, // modem 端口 
win32_desktopmonitor, // 显示器 
win32_displayconfiguration, // 显卡 
win32_displaycontrollerconfiguration, // 显卡设置 
win32_videocontroller, // 显卡细节。 
win32_videosettings, // 显卡支持的显示模式。 
 
// 操作系统 
win32_timezone, // 时区 
win32_systemdriver, // 驱动程序 
win32_diskpartition, // 磁盘分区 
win32_logicaldisk, // 逻辑磁盘 
win32_logicaldisktopartition, // 逻辑磁盘所在分区及始末位置。 
win32_logicalmemoryconfiguration, // 逻辑内存配置 
win32_pagefile, // 系统页文件信息 
win32_pagefilesetting, // 页文件设置 
win32_bootconfiguration, // 系统启动配置 
win32_computersystem, // 计算机信息简要 
win32_operatingsystem, // 操作系统信息 
win32_startupcommand, // 系统自动启动程序 
win32_service, // 系统安装的服务 
win32_group, // 系统管理组 
win32_groupuser, // 系统组帐号 
win32_useraccount, // 用户帐号 
win32_process, // 系统进程 
win32_thread, // 系统线程 
win32_share, // 共享 
win32_networkclient, // 已安装的网络客户端 
win32_networkprotocol, // 已安装的网络协议 

所有key:

win32_1394controller
win32_1394controllerdevice
win32_account
win32_accountsid
win32_ace
win32_actioncheck
win32_allocatedresource
win32_applicationcommandline
win32_applicationservice
win32_associatedbattery
win32_associatedprocessormemory
win32_baseboard
win32_baseservice
win32_battery
win32_binary
win32_bindimageaction
win32_bios
win32_bootconfiguration
win32_bus
win32_cachememory
win32_cdromdrive
win32_checkcheck
win32_cimlogicaldevicecimdatafile
win32_classiccomapplicationclasses
win32_classiccomclass
win32_classiccomclasssetting
win32_classiccomclasssettings
win32_classinfoaction
win32_clientapplicationsetting
win32_codecfile
win32_comapplication
win32_comapplicationclasses
win32_comapplicationsettings
win32_comclass
win32_comclassautoemulator
win32_comclassemulator
win32_commandlineaccess
win32_componentcategory
win32_computersystem
win32_computersystemprocessor
win32_computersystemproduct
win32_comsetting
win32_condition
win32_createfolderaction
win32_currentprobe
win32_dcomapplication
win32_dcomapplicationaccessallowedsetting
win32_dcomapplicationlaunchallowedsetting
win32_dcomapplicationsetting
win32_dependentservice
win32_desktop
win32_desktopmonitor
win32_devicebus
win32_devicememoryaddress
win32_devicesettings
win32_directory
win32_directoryspecification
win32_diskdrive
win32_diskdrivetodiskpartition
win32_diskpartition
win32_displayconfiguration
win32_displaycontrollerconfiguration
win32_dmachannel
win32_drivervxd
win32_duplicatefileaction
win32_environment
win32_environmentspecification
win32_extensioninfoaction
win32_fan
win32_filespecification
win32_floppycontroller
win32_floppydrive
win32_fontinfoaction
win32_group
win32_groupuser
win32_heatpipe
win32_idecontroller
win32_idecontrollerdevice
win32_implementedcategory
win32_infrareddevice
win32_inifilespecification
win32_installedsoftwareelement
win32_irqresource
win32_keyboard
win32_launchcondition
win32_loadordergroup
win32_loadordergroupservicedependencies
win32_loadordergroupservicemembers
win32_logicaldisk
win32_logicaldiskrootdirectory
win32_logicaldisktopartition
win32_logicalfileaccess
win32_logicalfileauditing
win32_logicalfilegroup
win32_logicalfileowner
win32_logicalfilesecuritysetting
win32_logicalmemoryconfiguration
win32_logicalprogramgroup
win32_logicalprogramgroupdirectory
win32_logicalprogramgroupitem
win32_logicalprogramgroupitemdatafile
win32_logicalshareaccess
win32_logicalshareauditing
win32_logicalsharesecuritysetting
win32_managedsystemelementresource
win32_memoryarray
win32_memoryarraylocation
win32_memorydevice
win32_memorydevicearray
win32_memorydevicelocation
win32_methodparameterclass
win32_mimeinfoaction
win32_motherboarddevice
win32_movefileaction
win32_msiresource
win32_networkadapter
win32_networkadapterconfiguration
win32_networkadaptersetting
win32_networkclient
win32_networkconnection
win32_networkloginprofile
win32_networkprotocol
win32_nteventlogfile
win32_ntlogevent
win32_ntlogeventcomputer
win32_ntlogeventlog
win32_ntlogeventuser
win32_odbcattribute
win32_odbcdatasourceattribute
win32_odbcdatasourcespecification
win32_odbcdriverattribute
win32_odbcdriversoftwareelement
win32_odbcdriverspecification
win32_odbcsourceattribute
win32_odbctranslatorspecification
win32_onboarddevice
win32_operatingsystem
win32_operatingsystemqfe
win32_osrecoveryconfiguration
win32_pagefile
win32_pagefileelementsetting
win32_pagefilesetting
win32_pagefileusage
win32_parallelport
win32_patch
win32_patchfile
win32_patchpackage
win32_pcmciacontroller
win32_perf
win32_perfrawdata
win32_perfrawdata_asp_activeserverpages
win32_perfrawdata_aspnet_114322_aspnetappsv114322
win32_perfrawdata_aspnet_114322_aspnetv114322
win32_perfrawdata_aspnet_aspnet
win32_perfrawdata_aspnet_aspnetapplications
win32_perfrawdata_ias_iasaccountingclients
win32_perfrawdata_ias_iasaccountingserver
win32_perfrawdata_ias_iasauthenticationclients
win32_perfrawdata_ias_iasauthenticationserver
win32_perfrawdata_inetinfo_internetinformationservicesglobal
win32_perfrawdata_msdtc_distributedtransactioncoordinator
win32_perfrawdata_msftpsvc_ftpservice
win32_perfrawdata_mssqlserver_sqlserveraccessmethods
win32_perfrawdata_mssqlserver_sqlserverbackupdevice
win32_perfrawdata_mssqlserver_sqlserverbuffermanager
win32_perfrawdata_mssqlserver_sqlserverbufferpartition
win32_perfrawdata_mssqlserver_sqlservercachemanager
win32_perfrawdata_mssqlserver_sqlserverdatabases
win32_perfrawdata_mssqlserver_sqlservergeneralstatistics
win32_perfrawdata_mssqlserver_sqlserverlatches
win32_perfrawdata_mssqlserver_sqlserverlocks
win32_perfrawdata_mssqlserver_sqlservermemorymanager
win32_perfrawdata_mssqlserver_sqlserverreplicationagents
win32_perfrawdata_mssqlserver_sqlserverreplicationdist
win32_perfrawdata_mssqlserver_sqlserverreplicationlogreader
win32_perfrawdata_mssqlserver_sqlserverreplicationmerge
win32_perfrawdata_mssqlserver_sqlserverreplicationsnapshot
win32_perfrawdata_mssqlserver_sqlserversqlstatistics
win32_perfrawdata_mssqlserver_sqlserverusersettable
win32_perfrawdata_netframework_netclrexceptions
win32_perfrawdata_netframework_netclrinterop
win32_perfrawdata_netframework_netclrjit
win32_perfrawdata_netframework_netclrloading
win32_perfrawdata_netframework_netclrlocksandthreads
win32_perfrawdata_netframework_netclrmemory
win32_perfrawdata_netframework_netclrremoting
win32_perfrawdata_netframework_netclrsecurity
win32_perfrawdata_outlook_outlook
win32_perfrawdata_perfdisk_physicaldisk
win32_perfrawdata_perfnet_browser
win32_perfrawdata_perfnet_redirector
win32_perfrawdata_perfnet_server
win32_perfrawdata_perfnet_serverworkqueues
win32_perfrawdata_perfos_cache
win32_perfrawdata_perfos_memory
win32_perfrawdata_perfos_objects
win32_perfrawdata_perfos_pagingfile
win32_perfrawdata_perfos_processor
win32_perfrawdata_perfos_system
win32_perfrawdata_perfproc_fullimage_costly
win32_perfrawdata_perfproc_image_costly
win32_perfrawdata_perfproc_jobobject
win32_perfrawdata_perfproc_jobobjectdetails
win32_perfrawdata_perfproc_process
win32_perfrawdata_perfproc_processaddressspace_costly
win32_perfrawdata_perfproc_thread
win32_perfrawdata_perfproc_threaddetails_costly
win32_perfrawdata_remoteaccess_rasport
win32_perfrawdata_remoteaccess_rastotal
win32_perfrawdata_rsvp_acsperrsvpservice
win32_perfrawdata_spooler_printqueue
win32_perfrawdata_tapisrv_telephony
win32_perfrawdata_tcpip_icmp
win32_perfrawdata_tcpip_ip
win32_perfrawdata_tcpip_nbtconnection
win32_perfrawdata_tcpip_networkinterface
win32_perfrawdata_tcpip_tcp
win32_perfrawdata_tcpip_udp
win32_perfrawdata_w3svc_webservice
win32_physicalmedia
win32_physicalmemory
win32_physicalmemoryarray
win32_physicalmemorylocation
win32_pnpallocatedresource
win32_pnpdevice
win32_pnpentity
win32_pointingdevice
win32_portablebattery
win32_portconnector
win32_portresource
win32_potsmodem
win32_potsmodemtoserialport
win32_powermanagementevent
win32_printer
win32_printerconfiguration
win32_printercontroller
win32_printerdriverdll
win32_printersetting
win32_printershare
win32_printjob
win32_privilegesstatus
win32_process
win32_processor
win32_processstartup
win32_product
win32_productcheck
win32_productresource
win32_productsoftwarefeatures
win32_progidspecification
win32_programgroup
win32_programgroupcontents
win32_programgrouporitem
win32_property
win32_protocolbinding
win32_publishcomponentaction
win32_quickfixengineering
win32_refrigeration
win32_registry
win32_registryaction
win32_removefileaction
win32_removeiniaction
win32_reservecost
win32_scheduledjob
win32_scsicontroller
win32_scsicontrollerdevice
win32_securitydescriptor
win32_securitysetting
win32_securitysettingaccess
win32_securitysettingauditing
win32_securitysettinggroup
win32_securitysettingoflogicalfile
win32_securitysettingoflogicalshare
win32_securitysettingofobject
win32_securitysettingowner
win32_selfregmoduleaction
win32_serialport
win32_serialportconfiguration
win32_serialportsetting
win32_service
win32_servicecontrol
win32_servicespecification
win32_servicespecificationservice
win32_settingcheck
win32_share
win32_sharetodirectory
win32_shortcutaction
win32_shortcutfile
win32_shortcutsap
win32_sid
win32_smbiosmemory
win32_softwareelement
win32_softwareelementaction
win32_softwareelementcheck
win32_softwareelementcondition
win32_softwareelementresource
win32_softwarefeature
win32_softwarefeatureaction
win32_softwarefeaturecheck
win32_softwarefeatureparent
win32_softwarefeaturesoftwareelements
win32_sounddevice
win32_startupcommand
win32_subdirectory
win32_systemaccount
win32_systembios
win32_systembootconfiguration
win32_systemdesktop
win32_systemdevices
win32_systemdriver
win32_systemdriverpnpentity
win32_systemenclosure
win32_systemloadordergroups
win32_systemlogicalmemoryconfiguration
win32_systemmemoryresource
win32_systemnetworkconnections
win32_systemoperatingsystem
win32_systempartitions
win32_systemprocesses
win32_systemprogramgroups
win32_systemresources
win32_systemservices
win32_systemsetting
win32_systemslot
win32_systemsystemdriver
win32_systemtimezone
win32_systemusers
win32_tapedrive
win32_temperatureprobe
win32_thread
win32_timezone
win32_trustee
win32_typelibraryaction
win32_uninterruptiblepowersupply
win32_usbcontroller
win32_usbcontrollerdevice
win32_useraccount
win32_userdesktop
win32_videoconfiguration
win32_videocontroller
win32_videosettings
win32_voltageprobe
win32_wmielementsetting
win32_wmisetting

到此这篇关于c# managementobjectsearcher操作window案例详解的文章就介绍到这了,更多相关c# managementobjectsearcher操作window内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!