- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- DriveInfo[] di=DriveInfo.GetDrives();
- foreach(DriveInfo d in di)
- {
- listBox1.Items.Add(d.Name);
- }
- }
- private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- DriveInfo di = new DriveInfo(listBox1.SelectedItem.ToString());
- MessageBox.Show("盘:" + di.Name + "空余:" + di.AvailableFreeSpace/1024/1024+"Mb"+ di.DriveFormat);
- }
- }
- }
在窗体上添加一个listBox控件,用于显示驱动器
主要使用DriveInfo类获取的,类中还有其他很多信息,大家可以试一试!