图05:添加引用 |
图06:添加引用 |
using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using System.Management ; //WMI操作类位于的命名空间 |
组件类型 | 组件名称 | 属性 | 设置结果 |
Form | Form1 | Text | 获取远程计算机硬盘信息 |
Form1 | FormBorderStyle | FixedSingle | |
Form1 | MaximizeBox | False | |
Label | label1 | Text | 远程计算机名或IP地址: |
label2 | Text | 拥有WMI权限的用户名: | |
label3 | Text | 口令: | |
TextBox | textBox1 | Text | "" |
textBox2 | Text | "" | |
textBox3 | PasswordChar | * | |
textBox3 | Text | "" | |
Button | button1 | Text | 获得硬盘信息 |
|
button1 | FlatStyle | Flat |
图07:【获得远程计算机硬盘信息】项目的设计界面 |
private void button1_Click ( object sender , System.EventArgs e) { long mb = 1048576 ; //1024x1024 //设定生成的WMI所需的所有设置 System.Management.ConnectionOptions Conn = new ConnectionOptions( ) ; //设定用于WMI连接操作的用户名 Conn.Username = textBox2.Text ; //设定用户的口令 Conn.Password = textBox3.Text ; //设定用于执行WMI操作的范围 System.Management.ManagementScope Ms = new ManagementScope ("\\\\" + textBox1.Text + "\\root\\cimv2" , Conn ) ; try { //连接到实际操作的WMI范围 Ms.Connect ( ) ; //设定通过WMI要查询的内容 ObjectQuery Query = new ObjectQuery ( "select FreeSpace ,Size,Name from Win32_LogicalDisk where DriveType=3" ) ; //WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合 ManagementObjectSearcher Searcher = newManagementObjectSearcher ( Ms , Query ) ; //异步调用WMI查询 ManagementObjectCollection ReturnCollection = Searcher.Get ( ); double free = 0 ; double use = 0 ; double total = 0 ; listBox1.Items.Clear ( ) ; //通过对产生的WMI的实例集合进行检索,获得硬盘信息 foreach ( ManagementObject Return in ReturnCollection ) { listBox1.Items.Add ( "磁盘名称:" + Return["Name"].ToString () ) ; //获得硬盘的可用空间 free = Convert.ToInt64 ( Return["FreeSpace"] ) /mb ; //获得硬盘的已用空间 use = ( Convert.ToInt64 ( Return["Size"] ) - Convert.ToInt64( Return["FreeSpace"] ) ) /mb ; //获得硬盘的合计空间 total = Convert.ToInt64 ( Return["Size"] ) /mb ; listBox1.Items.Add ( " 总计:"+ total.ToString ( ) + "MB" ); listBox1.Items.Add ( "已用空间:"+ use.ToString ( ) + "MB" ); listBox1.Items.Add ( "可用空间:"+ free.ToString ( ) + "MB" ); } } catch ( Exception ee ) { MessageBox.Show ( "连接" + textBox1.Text + "出错,出错信息为:"+ ee.Message ,"出现错误!" ) ; } } |
图08:【获得远程计算机硬盘信息】的运行界面 |