C#-获取磁盘空间

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;

namespace GetHardDiskSize
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            System.IO.DriveInfo[] drive = System.IO.DriveInfo.GetDrives();//获取所有驱动器
            for (int i = 0; i < drive.Length; i++)//遍历驱动器
            {
                comboBox1.Items.Add(drive[i].Name);//将驱动器添加到下拉列表中
            }
            comboBox1.SelectedIndex = 0;//设置下拉列表默认选择第一项
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                System.IO.DriveInfo[] drive = System.IO.DriveInfo.GetDrives();//获取所有驱动器
                for (int i = 0; i < drive.Length; i++)//遍历驱动器
                {
                    if (comboBox1.SelectedItem.ToString() == drive[i].Name)//判断遍历到的项是否与下拉列表中的项相同
                    {
                        label1.Text = "总空间:" + drive[i].TotalSize / 1024 / 1024 / 1024 + "G";//显示总空间
                        label2.Text = "剩余空间:" + drive[i].TotalFreeSpace / 1024 / 1024 / 1024 + "G";//显示剩余空间
                        label3.Text = "已用空间:" + (drive[i].TotalSize - drive[i].TotalFreeSpace) / 1024 / 1024 / 1024 + "G";//显示已用空间
                    }
                }
            }
            catch { }
        }
    }
}
 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值