WMI Code Generator How to Use WMI Code Creator Using Detailed Graphics Tutorial

Time:2019-10-7

WMI is a powerful tool for easy access to system information. Using it, but because of the lack of WMI’s introductory materials and learning materials (especially in Chinese), because most of the programmers in China are quite passable in E level, huh, I am also quite ready to do so, so I know that WMI is not enough and can be used very little.

Software Name:
WMI Code Creator 1.0 Chinese Green Free Edition
Software size:
52KB
Update time:
2018-04-24

WMI Code Creator is a WMI code generation tool provided by H.This article mainly introduces the use of WMI Code Creator. First of all, this software is compiled under. net Framework 2.0, so it needs. net Framework 2.0 support to run the software.

The interface of WMI code generator is as follows:

Using WMI code generator can execute query or execute a method, etc., it can also see the namespace and the class under the current or remote computer, and can also generate relevant code (otherwise it is not called WMI code generator), and support the generated code class including VBS, C# and VB.NET, and the default generated code is VBS.

For example, to query BIOS-related information, you can select root CIMV2 in the namespace (this is the default choice) and Win32_BIOS in all the classes. Then you can list all the properties of Win32_BIOS class. You can select one of the properties and click “Query Attribute Value”. Then you can list the values of the properties in the text box below, as shown in the figure below:

As can be seen from the figure above, my original BIOS is DELL. You can see the relevant VBS code at the right side of the generated code. If you look at the default generated VBS code, you can replace it with C# or VB.NET. The specific operation is to select it from the “Programming Language” menu, as shown in the following figure:

The following is the code generated by selecting the target programming language as C#, as shown in the following figure:

By default, WMI code generators connect to local machines, but also provide the ability to connect to remote hosts or host groups, which can be selected from the target machine. As shown in the figure below.

Since I have already sinicized some of the commonly used operations (which may not be very accurate), it is relatively easy to operate.

Note: Because thread pools are used in many places in the background to operate, exceptions will be reported when some time-consuming operations are performed without waiting for the complete termination of exit from the software. In addition, in the process of sinicization, some uncommon parts are not completely sinicized, and some translation is not very accurate, please forgive me, the results of using this software are only for reference. If your English is good enough, it is recommended that you download the English code to the English official website and compile it yourself (all the software is included in a CS file and need to be compiled under the command line). This software only provides some small help to those friends who are not very good at English.

WMI programming example in.net:

There is a query language in WMI, similar to the SQL language, called WQL (WMI Query Language), which is actually a subset of standard SQL with an extension of WMI. In the next tutorial, I will show you some examples of WQL.

With JS or vbs, system information can be obtained through WMI. Here is a small program written in VBS to get the serial number of the system. You can copy it into a text file, save it as a file of. vbs, and then run:

Set SNSet = GetObject(“winmgmts:”).InstancesOf (“Win32_OperatingSystem”)

for each SN in SNSet

MsgBox: “The serial number of the current operating system is:” & SN. Serial Number

This is the result of running on my machine:

How many lines of code would you need if you were asked to write this method in. net by conventional means? How many search engines do you need to use? I will show you how to use.Net to get the serial number of the operating system in WMI.

In fact, WMI programming is supported in. net. There is a system. Management namespace in. net class library. MSDN introduces this namespace as follows:

“Provides access to a large number of management information and management event sets related to the setting of checkpoints for systems, devices and applications in accordance with the Windows Management Specification (WMI) architecture. Applications and services can use classes derived from ManagementObjectSearcher and ManagementQuery to query interested management information (such as how much free space is left on disk, how much CPU utilization is currently, which database an application is connecting to, etc.) or applications and services can use the ManagementEventWatcher class to subscribe to various kinds of management. Event. These accessible data can come from managed and unmanaged components in a distributed environment.

The System. Management namespace contains the following common classes:

_Management Object or Management Class: Individual management objects or classes, respectively.

ManagementObjectSearcher: Used to retrieve a collection of ManagementObjects or ManagementClass objects based on a specified query or enumeration.

_Management Event Watcher: Used to reserve event notifications from WMI.

_Management Query: Used as the basis for all query classes.

In practical programming, it should be noted that the classes in the System. Management namespace exist in the System. Management. DLL file, so it is necessary to add a reference to the DLL file when programming. The following figure is as follows:

Then we need to add the following code at the beginning of our program: System. Management, so that we can use the classes under this namespace for WMI programming.

Next I will show you how to get the serial number of operating system in.Net by one way.

        /// <summary>

/ / / get the operating system serial number.

        /// </summary>

        /// <returns></returns>

        public string GetSerialNumber()

        {

            string result = “”;

            ManagementClass mClass = new ManagementClass(“Win32_OperatingSystem”);

            ManagementObjectCollection moCollection = mClass.GetInstances();

            foreach (ManagementObject mObject in moCollection)

            {

                result += mObject[“SerialNumber”].ToString();

            }

            return result;

        }

I think this method is simple enough.

Here are some common ways to get information about the system. The code is as follows:

using System;

using System.Collections.Generic;

using System.Text;

namespace WMIDemo

{

    /// <summary>

/// Note: This class is very simple and contains a Main method.

/// Author: Zhou Gong

/// Date: 2008-1-14

/// Initial address: http://blog.csdn.net/zhoufoxcn

    /// </summary>

    class Program

    {

        static void Main(string[] args)

        {

            GetSystemInfo getInfo = new GetSystemInfo();

Console. WriteLine (“serial number=”+getInfo. GetSerialNumber ()));

Console. WriteLine (“CPU number =” + getInfo. GetCpuID ()));

Console. WriteLine (“Hard Disk Number=” + getInfo. GetMainHardDiskId ()));

Console. WriteLine (“motherboard number =” + getInfo. GetMainBoardId ()));

Console. WriteLine (“Network Card Number=” + getInfo. GetNetwork Adapter Id ()));

Console. WriteLine (“User Group=” + getInfo. GetGroupName ()));

Console. WriteLine (“Driver Situation=” + getInfo. GetDriverInfo ()));

            Console.ReadLine();

        }

    }

}

Since it is a command-line program, there is the above class, and the following class contains the code I want to show:

using System;

using System.Collections.Generic;

using System.Text;

using System.Management;

namespace WMIDemo

{

    /// <summary>

/// Description: This class is mainly used to show how to use WMI to obtain some operating system information.

/// Author: Zhou Gong

/// Time: 2008-1-14

/// Initial address: http://blog.csdn.net/zhoufoxcn

    /// </summary>

    public class GetSystemInfo

    {

        /// <summary>

/ / / get the operating system serial number.

        /// </summary>

        /// <returns></returns>

        public string GetSerialNumber()

        {

            string result = “”;

            ManagementClass mClass = new ManagementClass(“Win32_OperatingSystem”);

            ManagementObjectCollection moCollection = mClass.GetInstances();

            foreach (ManagementObject mObject in moCollection)

            {

                result += mObject[“SerialNumber”].ToString() + ” “;

            }

            return result;

        }

        /// <summary>

/// Query CPU Number

        /// </summary>

        /// <returns></returns>

        public string GetCpuID()

        {

            string result = “”;

            ManagementClass mClass = new ManagementClass(“Win32_Processor”);

            ManagementObjectCollection moCollection = mClass.GetInstances();

            foreach (ManagementObject mObject in moCollection)

            {

                result += mObject[“ProcessorId”].ToString() + ” “;

            }

            return result;

        }

        /// <summary>

/// Query Hard Disk Number

        /// </summary>

        /// <returns></returns>

        public string GetMainHardDiskId()

        {

            string result = “”;

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(“SELECT * FROM Win32_PhysicalMedia”);

            ManagementObjectCollection moCollection = searcher.Get();

            foreach (ManagementObject mObject in moCollection)

            {

                result += mObject[“SerialNumber”].ToString() + ” “;

            }

            return result;

        }

        /// <summary>

/// motherboard number

        /// </summary>

        /// <returns></returns>

        public string GetMainBoardId()

        {

            string result = “”;

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(“root/CIMV2”,

                    “SELECT * FROM Win32_BaseBoard”);

            ManagementObjectCollection moCollection = searcher.Get();

            foreach (ManagementObject mObject in moCollection)

            {

                result += mObject[“SerialNumber”].ToString() + ” “;

            }

            return result;

        }

        /// <summary>

/// motherboard number

        /// </summary>

        /// <returns></returns>

        public string GetNetworkAdapterId()

        {

            string result = “”;

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(“SELECT MACAddress FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL)AND (Manufacturer <> ‘Microsoft’))”);

            ManagementObjectCollection moCollection = searcher.Get();

            foreach (ManagementObject mObject in moCollection)

            {

                result += mObject[“MACAddress”].ToString() + ” “;

            }

            return result;

        }

        /// <summary>

/// motherboard number

        /// </summary>

        /// <returns></returns>

        public string GetGroupName()

        {

            string result = “”;

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(“root/CIMV2”, “SELECT * FROM Win32_Group”);

            ManagementObjectCollection moCollection = searcher.Get();

            foreach (ManagementObject mObject in moCollection)

            {

                result += mObject[“Name”].ToString() + ” “;

            }

            return result;

        }

        /// <summary>

/// Get local drive information

        /// </summary>

        /// <returns></returns>

        public string GetDriverInfo()

        {

            string result = “”;

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(“root/CIMV2”, “SELECT * FROM Win32_LogicalDisk”);

            ManagementObjectCollection moCollection = searcher.Get();

            foreach (ManagementObject mObject in moCollection)

            {

// mObject [“DriveType”] has six possible values, representing the following meanings:

                //1:No type   2:Floppy disk   3:Hard disk

                //4:Removable drive or network drive   5:CD-ROM   6:RAM disk

// Only fixed drives (hard disk partitions) are listed here.

                if (mObject[“DriveType”].ToString() == “3”)

                {

                    result += string.Format(“Name={0},FileSystem={1},Size={2},FreeSpace={3} “, mObject[“Name”].ToString(),

                        mObject[“FileSystem”].ToString(), mObject[“Size”].ToString(), mObject[“FreeSpace”].ToString());

                }

            }

            return result;

        }

    }

}

The results of the program are as follows (the results on different machines will be different):

Finally, it is explained that although most Windows operating systems have already installed WMI (according to the official statement that the Windows system including WinME is installed after WinME), the above code can not be guaranteed to be above all Windows operating systems, such as Win95. If the WMI is running on Windows system which does not support WMI, then it is necessary to download the Windows from MSDN.

In addition, running WMI requires that the current Windows logged-in user have certain rights. Because I developed it as an administrator and run the program, there is no privilege exception prompt, but I do not guarantee that the code is running normally in your place, especially in the WebForm environment, because the asp. net program defaults to running with lower privileges (security reasons).

Recommended Today

Mongoose error: getaddrinfo ENOTFOUND localhost localhost:27017

{ Error: getaddrinfo ENOTFOUND localhost localhost:27017 at errnoException (dns.js:50:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26) code: ‘ENOTFOUND’, errno: ‘ENOTFOUND’, syscall: ‘getaddrinfo’, hostname: ‘localhost’, host: ‘localhost’, port: 27017 } mongoose.connect(‘mongodb://localhost:27017/db1’,{ poolSize:5, useNewUrlParser: true },err=>{ if(err){ console.error(err) }else { Console.log (‘mongodb successfully connected ‘) } }) Try changing localhost to 127.0.0.1 mongoose.connect(‘mongodb://127.0.0.1:27017/db1’,{ poolSize:5, useNewUrlParser: true },err=>{ if(err){ console.error(err) […]