第一种方法: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using driveInfo; /* Form1.Designer.cs private System.Windows.Forms.Button btnGetdriveinfo; private System.Windows.Forms.RichTextBox txtResult; */ namespace driveInfo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnGetdriveinfo_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); //声明DriveInfo类对象,并使用GetDrives方法取得目前 //系统中所有逻辑磁盘驱动器的DriveInfo类型的数组 DriveInfo[] myAllDrives = DriveInfo.GetDrives(); try { foreach (DriveInfo myDrive in myAllDrives) { if (myDrive.IsReady) { sb.Append("磁盘驱动器盘符:"); sb.AppendLine(myDrive.Name); sb.Append("磁盘卷标:"); sb.AppendLine(myDrive.VolumeLabel); sb.Append("磁盘类型:"); sb.AppendLine(myDrive.DriveType.ToString()); sb.Append("磁盘格式:"); sb.AppendLine(myDrive.DriveFormat); sb.Append("磁盘大小:"); sb.AppendLine(myDrive.TotalSize.ToString()); sb.Append("剩余空间:"); sb.AppendLine(myDrive.AvailableFreeSpace.ToString()); sb.Append("总剩余空间(含磁盘配额):"); sb.AppendLine(myDrive.TotalFreeSpace.ToString()); sb.AppendLine("-----------------------------------------"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } txtResult.Text = sb.ToString(); //把显示的磁盘信息写入RichTextBox } } } 第二种方法: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; /* Form1.Designer.cs private System.Windows.Forms.Button btnGetdriveInfo; private System.Windows.Forms.DataGridView drivesDataGridView; */ namespace driveinfo2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnGetdriveInfo_Click(object sender, EventArgs e) { drivesDataGridView.DataError += new DataGridViewDataErrorEventHandler(drivesDataGridView_DataError); this.drivesDataGridView.DataSource = DriveInfo.GetDrives(); } private void drivesDataGridView_DataError(System.Object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e) { // 并非所有的磁盘驱动器都是就绪的。 // 我们忽略错误。 } private void drivesDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { } } } 第三种方法: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; /* Form1.Designer.cs private System.Windows.Forms.Label label1; private System.Windows.Forms.ComboBox cboDrive; private System.Windows.Forms.PropertyGrid drivesInfoPropertyGrid; */ namespace driveinfo3 { public partial class form3 : Form { public form3() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DriveInfo[] myAllDrives = DriveInfo.GetDrives(); //系统中所有逻辑磁盘驱动器DriveInfo类型数组 this.cboDrive.Items.AddRange(myAllDrives); } private void cboDrive_SelectedIndexChanged(object sender, EventArgs e) { DriveInfo theDriveInfo = (DriveInfo)cboDrive.Items[cboDrive.SelectedIndex]; if (theDriveInfo.IsReady) { this.drivesInfoPropertyGrid.SelectedObject = cboDrive.Items[cboDrive.SelectedIndex]; } else { MessageBox.Show(theDriveInfo.Name + "磁盘尚未就绪", "请注意", MessageBoxButtons.OK); this.drivesInfoPropertyGrid.SelectedObject = null; } } private void drivesInfoPropertyGrid_Click(object sender, EventArgs e) { } } }