form1.cs

来自「csharp课本的源代码」· CS 代码 · 共 108 行

CS
108
字号
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;

namespace DriverInfoExample
{
    public partial class Form1 : Form
    {
        private DirectoryInfo dirInfo;
        private long totalSpace;
        private long freeSpace;
        private long usedSpace;
        private float sweep;
        private float freepercent;
        private float usepercent;
        private bool isSpaceInfoAvailable;

        public Form1()
        {
            InitializeComponent();
            lableDriveReadyStatus.Text = "";
            DriveInfo[] drivers = DriveInfo.GetDrives();
            comboBoxDrivesOnPc.Items.AddRange(drivers);

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Rectangle rect = new Rectangle(370, 20, 200, 200);
            Rectangle rect2 = new Rectangle(310, 10, 320, 320);
            Rectangle freeLegend = new Rectangle(320, 250, 20, 20);
            Rectangle usedLegend = new Rectangle(320, 275, 20, 20);
            e.Graphics.DrawRectangle(Pens.Black, rect2);
            if (isSpaceInfoAvailable == true)
            {
                e.Graphics.FillPie(Brushes.Green, rect, 0, sweep);
                e.Graphics.FillPie(Brushes.Red, rect, sweep, 360 - sweep);
                e.Graphics.FillRectangle(Brushes.Green, freeLegend);
                e.Graphics.FillRectangle(Brushes.Red, usedLegend);
            }

        }
        private void LoadDriveInfo(string driveLetter)
        {
            DriveInfo driveInfo;
            try
            {
                driveInfo = new DriveInfo(driveLetter);
            }
            catch (ArgumentNullException ex1)
            {
                MessageBox.Show("The drive letter can not be null./n/r" + ex1.Message,
"Drive Letter error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            catch (ArgumentException ex2)
            {
                MessageBox.Show("The drive letter must be in the range of a-z./n/r" + ex2.Message, "Drive Letter error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.textBoxDriveName.Text = driveInfo.Name;
            try
            {
                if (driveInfo.VolumeLabel.Length > 0)
                    this.textBoxDriveVolumeLabel.Text = driveInfo.VolumeLabel;
                else
                    this.textBoxDriveVolumeLabel.Text = "None";
                this.textBoxDriveFormat.Text = driveInfo.DriveFormat;
                totalSpace = driveInfo.TotalSize;
                freeSpace = driveInfo.TotalFreeSpace;
                usedSpace = totalSpace - freeSpace;
                sweep = 360f * freeSpace / totalSpace;
                freepercent = (int)(100f * freeSpace / totalSpace);
                usepercent = (int)(100f * usedSpace / totalSpace);
                isSpaceInfoAvailable = true;
            }
            catch
            {
                this.textBoxDriveVolumeLabel.Text = "Not available";
                this.textBoxDriveFormat.Text = "Not available";
                isSpaceInfoAvailable = false;
            }
            this.textBoxDriveType.Text = driveInfo.DriveType.ToString();
            this.textBoxDriveRootDirectory.Text = driveInfo.RootDirectory.ToString();
            dirInfo = driveInfo.RootDirectory;
            if (driveInfo.IsReady == true)
                this.lableDriveReadyStatus.Text = "Drive is Ready";
            else
                this.lableDriveReadyStatus.Text = "Drive is NOT Ready";
        }

        private void comboBoxDriveOnPc_SelectionChangeCommitted(object sender, EventArgs e)
        {
            LoadDriveInfo(comboBoxDrivesOnPc.Items[comboBoxDrivesOnPc.SelectedIndex].ToString());
            label7.Text = "可用空间:" + freepercent.ToString() + "%";
            label8.Text = "已用空间:" + usepercent.ToString() + "%";
            label9.Text = "分区总容量:" + (int)(totalSpace / 1073741824) + "GB";
            this.Invalidate();

        }

    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?