WMI is a more confused thing for most developers, and it is true for me. Until recently, after the introduction of .Net Management, WMI has a little understanding. Let's write your own experience to friends who love C #.
WMI is a very powerful tool that provides us with very powerful features. For example, when we develop applications, you often need information from local or remote operating systems. Although it looks usually, it is still more troublesome, especially when we want to get information about the remote operating system. WMI requires developers to use WQL ways to access each other. WQL allows us to use it like using the SQL-Link query language. As long as you know these classes and its field variables, you can easily get what you want. Using WMI, users can accurately define data streams with queries. The query limits the amount of data returned by specifying the items (in the SELECT statement) and only the entity that is interested (using the WHERE clause). For example, if the user wants to retrieve all of the computer (Drive C, D, etc.) on the computer:
Select * from win32_logicaldisk Where FreeSpace <2000000
As you can see, using a simple WMI query is quite easy. In order to achieve this, you need to reference the system.management namespace in .NET. As long as you know a bit of database knowledge, we can use it to do all our queries.
The system.management namespace is WMI namespace in the .NET framework. This namespace includes the following objects:
ManagementObject or ManagementClass: Single management object or class separately.
ManagementObjectSearcher: Used to retrieve the collection of ManagementObject or Management or Management Class objects based on the specified query or enumerate.
Managementeventwatcher: It is used to book event notifications from WMI.
ManagementQuery: Used as the basis for all query classes.
The following codes show you how to use WMI to list all open processes on the remote machine.
Private Void RemoteEnumerate_Click (Object Sender, System.Eventargs E)
{
/ / If you are accessing to the remote machine, you must connect, you can omit the local
ConnectionOptions Oconn = New ConnectionOptions ();
// Username and password are that you have entered the other party, that is, the other party gives you Oconn.userName = "zhangdong"; // Access the other party Oconn.Password = "1234"; // Access each other Password
// ServerName is the name of the other machine, or it can be an IP address, // root // CIMV2 photo, managementPath P = New ManagementPath ("ServerName // Root // CIMv2);
Managementscope ms = new managementScope (p, op);
ObjectQuery oq = New ObjectQuery ("Select * from win32_process");
ManagementObjectSearcher Query1 = New ManagementSearcher (MS, OQ);
ManagementObjectCollection QueryCollection1 = query1.get (); // Replace all ports and displayed in ListBox
Forum (ManagementObject Service in QueryCollection1)
{
/ / Displayed in the list box is the name and path of the process. Of course, there are other information, such as process handle, // process priority, now run status, we are not all listed, interested, you can self / / Try
ListBox1.Items.Add ("Service:" Service ["Name"] "FilePath:" Service ["ExecutablePath"]);
}
As you can see, it is not difficult to achieve it, it can be said that it is easy.
WMI method calls WMI another more interesting aspect is a remote method call. Of course, the method is present in each WMI class, WMI
The class has its own method, and there is no way to have a WMI class. Let's take a look at the other WMI class. Win32_share class, this WMI class has three methods for: create, delete, setshareInfo We only talk about a CREATE method, the remaining two and other WMI methods are the same.
Where the CREATE method illustrates:
Int crete
(String SharePath; // Sharing path, you have to share the path
String ShareName; // After the shared, the name of the log name is
INT ShareType; // Share type, generally assigned 0, indicating disk sharing
Int MaxUsers; // Maximum number of users
String description; // Description
String password; // Access password
)
Returns 0 means successfully created sharing. After the following program creates a share, only when you click the right button to see the sharing to see this folder is already shared, otherwise you can't see it. Simply shared, system-level sharing.
Private void Remoteshare_Click (Object Sender, System.Eventargs E)
{// Connect the remote computer, we want to operate it, you must first connect
ConnectionOptions myconnect = new connectionOptions ();
MyConnect.username = "zhangdong";
MyConnect.password = "1234";
ManagementPath Remotenamespace = New ManagementPath ("ServerName // Root // CIMv2");
Managementscope myscope = new managementScope (RemotenameSpace, MyConnect);
// First connect the WMI class to reference here, here we reference the win32_share class
ManagementPath SharePath = New ManagementPath ("Win32_Share");
ObjectGetoptions OtherOption = New ObjectGetoptions (Null, New
Timespan (0,0,10), true);
ManagementClass _ProcessClass = New ManagementClass (MyScope, SharePath, OtherOption);
Object [] Sharea = {"C: // Program Files", "My Sharing", 0, 10, "DOT NET implementation", "}"
Object Result = _ProcessClass.InvokeMethod ("Create", Sharea);
Messagebox (0, "The value returned is:" Result.toString () shared name: " Sharea [1]," Sharing Information ", 0);
What is the point we need to pay attention to the above procedure is the form of the parameter, we use this form
Object [] Sharea = {"C: // Program Files", "My Sharing", 0, 10, "DOT NET implementation", "}"
This array represents the parameters transmitted in the method, and other method calls in the WMI class can refer to this form.
For example, before we talk about it, you can list all the processes that open, if we want to close an open process, we need to implement the following code:
Forum (ManagementObject Service in QueryCollection1)
{// Judgment Whether the Delphi program is turned on, if yes, turn it off
IF (service ["name"]. TOSTRING () == "Delphi32.exe")
{
String [] tpaas = {"0"}; // parameter array
Service.InvokeMethod ("Terminate", TPARAS; // Termination method
}
/ / List all the names of all processes, and the path to this application, of course, there are other properties, but this // is not listed in this //
ListBox1.Items.Add ("Service:" Service ["Name"] "FilePath:" Service ["ExecutablePath"]);
}
to sum up:
About WMI still has many other features, I hope that you can understand the Windows management in .NET. At the same time, I also hope that friends with C # communicate with each other.