用WMI
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
/// <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.SuspendLayout();
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(96, 208);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// listBox1
// 
this.listBox1.HorizontalScrollbar = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(16, 16);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(152, 88);
this.listBox1.TabIndex = 1;
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(688, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
  this.listBox1,
  this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} /// <summary>
/// 查看本机磁盘的信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, System.EventArgs e)
{
//因为不好在foreach中使用for循环,所以在这里定义好i的值
int i=1;
//用来放置磁盘信息,目前以本机的磁盘分区情况做死(有6个,但第一个用来显示标题,所以长度为7),以后做活
String[] strContainer={"","","","","","",""};
            strContainer[0]="磁盘名称  磁盘类型  逻辑卷名";
SelectQuery query=new SelectQuery("Select * From Win32_LogicalDisk");
ManagementObjectSearcher searcher=new ManagementObjectSearcher(query);
//获取各个逻辑磁盘信息
foreach(ManagementBaseObject disk in searcher.Get())
{
strContainer[i]=(disk["Name"] + " "  + disk["DriveType"] + " " + disk["VolumeName"]);
i++;
}
listBox1.DataSource=strContainer;

}
}
}

解决方案 »

  1.   

    using System.Management;
    D:\重要数据\MY\WinCloneDVD\Mui\Mui\Form3.cs(6): 类型或命名空间名称“Management”在类或命名空间“System”中不存在(是否缺少程序集引用?)
    怎么办呀?
      

  2.   

    using System.IO;// list available logical drives
    string[] Drives;
    Drives = Directory.GetLogicalDrives();
    foreach (string Drive in Drives)
    {
       Console.WriteLine(Drive);
    }
      

  3.   

    在项目的“引用”点右键,选择“添加引用”
    在“.NET”卡里面找到System.Managemen,“选择“,单击确定就可以了