获取远程计算机硬盘信息

作者:网际浪子

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;

namespace 获得远程计算机硬盘信息
{
    /// <summary> 
    /// Form1 的摘要说明。 
    /// </summary> 
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox3;
        /// <summary> 
        /// 必需的设计器变量。 
        /// </summary> 
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            // 
            // Windows 窗体设计器支持所必需的 
            // 
            InitializeComponent();

            // 
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
            // 
        }

        /// <summary> 
        /// 清理所有正在使用的资源。 
        /// </summary> 
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code 
        /// <summary> 
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改 
        /// 此方法的内容。 
        /// </summary> 
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // button1 
            // 
            this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.button1.Location = new System.Drawing.Point(120, 328);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(110, 32);
            this.button1.TabIndex = 0;
            this.button1.Text = "获得硬盘信息";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // listBox1 
            // 
            this.listBox1.Location = new System.Drawing.Point(13, 112);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(307, 199);
            this.listBox1.TabIndex = 1;
            // 
            // label1 
            // 
            this.label1.Location = new System.Drawing.Point(16, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(142, 23);
            this.label1.TabIndex = 2;
            this.label1.Text = "远程计算机名或IP地址:";
            // 
            // label2 
            // 
            this.label2.Location = new System.Drawing.Point(16, 48);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(144, 23);
            this.label2.TabIndex = 3;
            this.label2.Text = "拥有WMI权限的用户名:";
            // 
            // label3 
            // 
            this.label3.Location = new System.Drawing.Point(112, 80);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(46, 23);
            this.label3.TabIndex = 4;
            this.label3.Text = "口令:";
            // 
            // textBox1 
            // 
            this.textBox1.Location = new System.Drawing.Point(168, 16);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(152, 20);
            this.textBox1.TabIndex = 5;
            this.textBox1.Text = "";
            // 
            // textBox2 
            // 
            this.textBox2.Location = new System.Drawing.Point(168, 48);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(152, 20);
            this.textBox2.TabIndex = 6;
            this.textBox2.Text = "";
            // 
            // textBox3 
            // 
            this.textBox3.Location = new System.Drawing.Point(168, 80);
            this.textBox3.Name = "textBox3";
            this.textBox3.PasswordChar = '*';
            this.textBox3.Size = new System.Drawing.Size(152, 20);
            this.textBox3.TabIndex = 7;
            this.textBox3.Text = "";
            // 
            // Form1 
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(336, 373);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                            this.textBox3,
                            this.textBox2,
                            this.textBox1,
                            this.listBox1,
                            this.button1,
                            this.label3,
                            this.label2,
                            this.label1});
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.Text = "获取远程计算机硬盘信息";
            this.ResumeLayout(false);
        }
        #endregion

        /// <summary> 
        /// 应用程序的主入口点。 
        /// </summary> 
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            long mb = 1048576; //1024x1024 
                               //设定生成的WMI所需的所有设置 
            System.Management.ConnectionOptions Conn = new ConnectionOptions();
            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 = new ManagementObjectSearcher(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, "出现错误!");
            }
        }
    }
}

Powered by DvNews.net

来源:uncj.net 阅读:197 次 日期:2003-6-30

Contributors: FHL