managing information queries and method access for objects
In this section, we'll take a few examples to learn how to query management object information and access the methods provided by the Admin object, which will be used in the relevant class objects in the System.Management namespace we covered earlier.
managing information queries for objects There are two ways to manage the information query of the object, one is to enumerate the various attributes of the management object directly, and get the data that we are interested in, one method is to filter the redundant data by the way of SQL query statement, and get the data of interest. For a Win32_LogicalDisk instance object, it has those attributes, that is, it has the information available for us to access. What these property names are. This is a problem that developers often encounter during the development process, and usually we can check MSDN, but here I give a programmatic way to get the code. Get Win32_LogicalDisk WMI Object accessible property public static void Fetchproperties () { ManagementClass diskclass = new ManagementClass ( "Win32_LogicalDisk"); Diskclass.get (); Console.WriteLine ("Win32_LogicalDisk object has a" + DiskClass.Properties.Count + "property.) "); . propertydatacollection diskproperties = diskclass.properties; foreach (Propertydata diskproperty in diskproperties) { Console.WriteLine ("Diskclass[{0}" ] ", diskproperty.name); } } ***************************************************** Knowing that those attributes are available for us to access, the next step is to specifically get information about the management object Win32_LogicalDisk. The first method, the enumeration attribute, is used to go back to the data. Here we get the various logical partitions on your computer, including the floppy disk drive and the optical drive, and output some information about each logical partition by enumerating the data of the WMI class Win32_LogicalDisk instance objects. Gets the data information for the Win32_LogicalDisk object public static void Fetchinfo () { ManagementClass diskclass = new ManagementClass ("Win32_LogicalDisk"); managementobjectcollection disks = Diskclass.getinstances (); foreach (ManagementObject disk in disks) { Console.WriteLine ("disk_deviceid= {0}", disk["DeviceID"]. ToString ()); Console.WriteLine ("Disk_name = {0}", disk["Name"). ToString ()); if (disk["filesystem"]!=null) Console.WriteLine ("Disk_filesystem = {0}" ", disk[" filesystem "]. ToString ()); Console.WriteLine ("Disk_ Description = {0} ", disk[" Description "]. ToString ()); if (disk["Size"]!=null) Console.WriteLine ("Disk_size ={0}", System.Convert.ToInt64 (disk["Size"). ToString ())); Console.WriteLine ("Disk_type = {0}" ", System.Convert.ToInt16 (disk[" DriveType "). ToString ()); } ***************************************************** Hint: (1) Because of your floppy drive and CD-ROM filesystem and Size property is empty, all added if judgment, otherwise the output will produce an exception. The data for the (2) Size property is in bytes bytes. The method above is actually very simple, so let's look at how to access the data of the Admin object through the SQL query. The following example is an example of a Win32_Process management object. Accessing admin object data via SQL query public static void QueryInfo () { ManagementObjectSearcher searcher = new & nbsp; managementobjectsearcher ("SELECT * from Win32_Process"); foreach (ManagementObject process in searcher. Get ()) { ConsOle. WriteLine ("process =" + process["Name"]); } } At this point, we can easily access the management object's data through the above way.
managing access to Object methodsIn Windows2000 's task Manager, we can terminate a process or start a process; In the Service Manager, we can start, pause, and 7 terminate the service, and you will certainly ask how the program accomplishes the task similar to the above. Yes, for those management objects that are available to the user, it provides a number of public methods that can be invoked by the client application to accomplish a variety of tasks. The above example just gives a one-way access to information, and the next thing we'll look at is how to access the methods of managing objects. Not all management objects expose methods, whether exposing methods and exposing those methods depends on the need. For logical disks, a method that exposes a format may be used to format the disk, and for a running process, the start and stop methods may be exposed to begin terminating the process. We'll take the example of starting a process to illustrate this issue. To create a process, you need to invoke the Create method of the Win32_Process WMI object. The Create method has four input parameters, respectively: UInt32 Create (String CommandLine, String currentdirectory, Win32_ProcessStartup Processstartupin Formation, uint32* ProcessID); The CommandLine parameter is the full name of the process's executable file, such as starting a Notepad Notepad.exe; CurrentDirectory is the current working directory of the process being started and, if NULL, a working directory is shared between the child process and the parent process; Processstartupinformation: Process-initiated configuration information for reference to other materials; ProcessID: The globally unique identity ID of the process, which is assigned when the process is created. Instead of invoking the Create method directly in our client management application to start the process, we call ManagementClass. InvokeMethod () method to indirectly start the process. The InvokeMethod () method has two prototypes, one is an array of input parameters, one is an input parameter is a Managementbaseobject object, and for simplicity's sake, What we're using here is the Managementbaseobject object as input and output parameters.
Public Managementbaseobject InvokeMethod (string, Managementbaseobject, invokemethodoptions); Invokemethodoptions is the option to invoke the method, including call latency, and so on. Because there are so many WMI objects, and the exposed methods of each managed WMI object are different, the InvokeMethod () method is how to differentiate between these different WMI objects and the different public methods of WMI objects, because the difference in the method means that the parameter is different. So we also need to know the parameters of the Create-specific startup process method. ManagementClass. Getmethodparameters () gets different method input and output parameters at run time. To solve these critical issues, let's look at how the code is implemented. Manage method calls to the object public static void InvokeMethod () { managementclass processclass = new ManagementClass (" Win32_Process "); //Get input parameters Managementbaseobject inparams = processclass.getmethodparameters ("Create"); inparams[" CommandLine "] =" notepad.exe "; //HoldsRow creation Process Managementbaseobject outparams = Processclass.invokemethod (" Create ", inparams, NULL); //Show results of the Create process Console.WriteLine ("Creation of calculator process returned:" + outparams["returnvalue"]); Console.WriteLine ("Process ID:" + outparams["ProcessID"]); } ***************************************************** Look at the results of the run, open a Notepad program, and output similar to the following information: Creation of calculator process returned:0 process id:2340