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

解决方案 »

  1.   

    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(472, 172);
    this.listBox1.TabIndex = 1;
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(688, 273);
    this.Controls.Add(this.listBox1);
    this.Controls.Add(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;

    }
    }
    }
      

  2.   

    哦,看到了,你添加个"System.Managemen"的引用就可以了