Obtaining remote computer information in Visual C # using WMI

Source: Internet
Author: User
Tags connect reference tostring visual studio
If you do not use WMI to obtain system data for a remote computer, the most common approach is to run a client program on a remote computer that is used by the local machine to obtain system data from the remote computer. This implementation is difficult both in programming and in the subsequent distribution of programs. And using WMI, everything seems very simple. The following is an example of the function of using WMI to get data for a remote computer's hard disk. You can get other data on the remote computer by simply modifying the program. The following are the specific implementation steps:

   Copyright Notice: any access to Tenkine authorized website, reprint, please be sure to keep the author information and the following links
Pole Development asp.net column: HTTP://DEV.YESKY.COM/MSDN/MSDNC

1. Start visual Studio. Net, select the file, new, Project menu, and then set the project type to Visual C # project in the New Project dialog box, set the template to Windows application, and in the name Enter the "Get remote computer hard drive Information" in the text box and type "E:\VS" in the "Location" text box. NET project, and then click the OK button. So in "E:\VS." NET project directory, you create a new folder, "Get remote computer hard drive information," which contains the project file "Get remote computer hard drive information."

2. Because of visual Studio. NET default compilation environment does not include the file "System.Management.dll" in which the namespace "System.Management" is located, so first introduce this DLL file into the project file. The specific steps are as follows:

Select References in Solution Explorer, right-click, and in the pop-up menu, select Add Reference, as shown in Figure 05:


Figure 05: Adding a reference
In the Add Reference dialog box, select the. Net page, select System.managemen in the Component Name field, click the Select button, add "System.managemen" in the Selected Components column, and then click the OK button. , then in Visual Studio. NET integrated development environment, the namespace "System.managemen" is introduced. As shown in Figure 06:


Figure 06: Adding a reference
3. In the Solution Explorer window, double-click the Form1.cs file to enter the editing interface of the Form1.cs file.

4. At the beginning of the Form1.cs file, replace the system default import namespace code with the following import namespace code.

Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using System.Management;
The WMI action class is in the namespace 5. Take visual Studio. NET Current window to the Form1.cs window, and from the Windows Forms component in the Toolbox, drag the following components into the form and perform the appropriate actions:

Three textbox components, enter the remote computer name or IP address, and the remote computer's Superuser account and password.

A ListBox component that displays remote computer hard disk information.

Three label components.

A button component, named Button1, after dragging into the form, double-clicking the component, the system automatically produces the processing code for its click event in Form1.cs.

6. Adjust the values of each component's properties according to the data in table 05:

Component type Component Name Property Setting the results
Form Form1 Text Get remote computer hard drive information
Form1 FormBorderStyle FixedSingle
Form1 MaximizeBox False
Label Label1 Text remote computer name or IP address:
Label2 Text User name with WMI permissions:
Label3 Text Password:
Textbox TextBox1 Text ""
TextBox2 Text ""
TextBox3 PasswordChar *
TextBox3 Text ""
Button Button1 Text Get Hard Drive information
Button1 FlatStyle Flat
Table 05: "Get remote computer hard drive Information" project form the main properties of each component set the value table

and adjust the position and order of the components in the form according to the placement and arrangement of the components in Figure 07:


Figure 07: The design interface for the "Get remote computer hard drive Information" project
7. Take visual Studio. NET switches to the editing window of the Form1.cs file and replaces the processing code for the Button1 click event in Form1.cs with the following code. The following code is a WMI query for a remote computer, a query to get the data, and a result to display:

private void Button1_Click (object sender, System.EventArgs e)
{
long MB = 1048576;
1024x1024
To set all the settings required for the generated WMI
System.Management.ConnectionOptions Conn = new ConnectionOptions ();
Set the user name for the WMI connection operation
Conn.username = TextBox2.Text;
Set the user's password
Conn.password = TextBox3.Text;
Set the scope used to perform WMI operations
System.Management.ManagementScope Ms = new Managementscope ("\\\\" + TextBox1.Text + "\\root\\cimv2", Conn);
Try
{
Connect to the WMI scope of the actual operation
Ms.connect ();
Set the content to be queried through WMI
ObjectQuery Query = new ObjectQuery ("Select FreeSpace, Size, Name from Win32_LogicalDisk where drivetype=3");
WQL statements, set WMI query content, and WMI's scope of operations, retrieving a collection of WMI objects
ManagementObjectSearcher Searcher = new ManagementObjectSearcher (Ms, Query);
To invoke a WMI query asynchronously
Managementobjectcollection returncollection = Searcher.get ();
Double free = 0;
Double use = 0;
Double total = 0;
ListBox1.Items.Clear ();
Retrieve the hard drive information by retrieving the collection of instances of the generated WMI
foreach (ManagementObject return in returncollection)
{
LISTBOX1.ITEMS.ADD ("Disk name:" + return["name"]. ToString ());
Get free space on your hard disk
Free = Convert.toint64 (return["FreeSpace"])/mb;
Get the used space on your hard disk
Use = (Convert.toint64 (return["Size"])-Convert.toint64 (return["FreeSpace"))/MB;
Get the total space on your hard disk
Total = Convert.toint64 (return["Size"])/mb;
LISTBOX1.ITEMS.ADD ("sum:" + total. ToString () + "MB");
LISTBOX1.ITEMS.ADD ("Used space:" + use.) ToString () + "MB");
LISTBOX1.ITEMS.ADD ("Available space:" + free.) ToString () + "MB");
}
}
catch (Exception ee)
{
MessageBox.Show ("Connection" + TextBox1.Text + "error, error message is:" + EE. Message, "There is an error!" " ) ;
}
}
8. At this point, all the work on the "Get remote computer hard drive Information" project is complete after the steps are completed correctly. Click the shortcut key F5 to run the program. After you correctly enter the IP address or user name of the remote computer, the remote computer Super username and password, and then click the Get Hard disk information button, the program gets the data for the hard disk of the specified computer and displays it, as shown in Figure 08:


Figure 08: "Get remote computer hard drive information" running interface
How to do, whether there is a sense of hacking. The following is an introduction to using WMI to accomplish a more "cool" program. This program will reboot or shut down the remote computer.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.