How to use managementobject
This article mainly shows you "how to use managementobject", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use managementobject" this article.
Managementobject begins:
The main class we use is ManagementObjectSearcher, which is under the System.Management namespace.
Sometimes we can get some simple system information through Environment.
For example, Environment.MachineName; gets the computer name.
Environment.UserName; gets the operating system login user name.
However, this article mainly discusses ManagementObjectSearcher's access to computer hardware and operating system information.
Steps for managementobject usage:
Add reference: System.Management
Introduce the namespace: usingSystem.Management
Create a ManagementObjectSearcher object
AnagementObjectSearchersearcher=newManagementObjectSearcher ("select*from" + Key)
For the key, see the following key list:
Get the ManagementObjectCollection collection through searcher.Get ()
Traversing the ManagementObjectCollection collection to get ManagementObject
Get the desired attribute through managementObject [name] or ManagementObject.GetPropertyValue (name)
If you don't know what to write about the name here, you can traverse and print it:
Foreach (varpropertyinmanagementObject.Properties)
{
Console.WriteLine (property.Name+ ":" + property.Value)
}
one
two
three
four
OK.
Example:
/ / get the CPU serial number
PublicstringGetCPUSerialNumber ()
{
Try
{
ManagementObjectSearchersearcher=newManagementObjectSearcher ("Select*FromWin32_Processor")
StringsCPUSerialNumber= ""
Foreach (ManagementObjectmoinsearcher.Get ())
{
SCPUSerialNumber=mo ["ProcessorId"] .ToString () .Trim ()
Break
}
ReturnsCPUSerialNumber
}
Catch
{
Return ""
}
}
The above is all the contents of this article "how to use managementobject". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!