2017年2月17日 星期五

[研究] [C#] [ASP.NET] 取得硬碟剩餘空間 (Windows 2016)

[研究] [C#] [ASP.NET] 取得硬碟剩餘空間 (Windows 2016)

2017-02-17

Visual Studio 2015 with Update 3
Windows Server 2016

測試發行到 Windows Server 2016 的 IIS 上可以跑

Default.aspx.cs
using System;
using System.IO;

namespace WebApplication2
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DriveInfo[] allDrives = DriveInfo.GetDrives();

            foreach (DriveInfo d in allDrives)
            {
                Label1.Text = Label1.Text + "----------------------------------------" + "<br />";
                Label1.Text = Label1.Text + "Drive : " + d.Name + "<br />";
                Label1.Text = Label1.Text + "Drive Type : " + d.DriveType + "<br />";
                if (d.IsReady == true)
                {
                    Label1.Text = Label1.Text + "Volume label : " + d.VolumeLabel + "<br />";
                    Label1.Text = Label1.Text + "File system : " + d.DriveFormat + "<br />";

                    Label1.Text = Label1.Text + "Available space : " + d.AvailableFreeSpace.ToString("###,###,###,###,###.##") + " Bytes <br />";
                    Label1.Text = Label1.Text + "Available space : " + (Decimal.Divide(d.AvailableFreeSpace, 1024) ).ToString("###,###,###,###,###.##") + " KB <br /> ";
                    Label1.Text = Label1.Text + "Available space : " + (Decimal.Divide(d.AvailableFreeSpace, 1024) / 1024).ToString("###,###,###,###,###.##") + " MB <br /> ";
                    Label1.Text = Label1.Text + "Available space : " + ((double)d.AvailableFreeSpace / 1024 / 1024 / 1024).ToString("###,###,###,###,###.##") + " GB <br /> ";
                    Label1.Text = Label1.Text + "Available space : " + (d.AvailableFreeSpace / 1024.0 / 1024 / 1024 / 1024).ToString("###,###,###,###,###.##") + " TB <br /> ";

                    Label1.Text = Label1.Text + "Total available space : " + d.TotalFreeSpace.ToString("###,###,###,###,###") + " Bytes<br />";
                    Label1.Text = Label1.Text + "Total size of drive : " + d.TotalSize.ToString("###,###,###,###,###") + " Bytes<br />";
                }
            }
            Label1.Text = Label1.Text + "----------------------------------------" + "<br />";
        }
    }
}


結果

----------------------------------------
Drive : C:\
Drive Type : Fixed
Volume label :
File system : NTFS
Available space : 47,580,495,872 Bytes
Available space : 46,465,328 KB
Available space : 45,376.3 MB
Available space : 44.31 GB
Available space : .04 TB
Total available space : 47,580,495,872 Bytes
Total size of drive : 125,304,827,904 Bytes
----------------------------------------
Drive : D:\
Drive Type : Fixed
Volume label :
File system : NTFS
Available space : 19,944,185,856 Bytes
Available space : 19,476,744 KB
Available space : 19,020.26 MB
Available space : 18.57 GB
Available space : .02 TB
Total available space : 19,944,185,856 Bytes
Total size of drive : 374,275,567,616 Bytes
----------------------------------------
Drive : E:\
Drive Type : Removable
----------------------------------------
Drive : F:\
Drive Type : Removable
----------------------------------------
Drive : G:\
Drive Type : Removable
----------------------------------------
Drive : H:\
Drive Type : Removable
----------------------------------------
Drive : I:\
Drive Type : CDRom
----------------------------------------


判斷 C: 和 D: 剩餘空間是否小於  10 GB (否則連其他磁碟都測試下去,會判斷錯誤)
bool enough = true;

DriveInfo[] allDrives = DriveInfo.GetDrives();
double space = 0.0;
double driveCSpace = 0.0;
double driveDSpace = 0.0;
foreach (DriveInfo d in allDrives)
{
    if (d.IsReady == true)
    {
        space = d.AvailableFreeSpace / 1024 / 1024 / 1024;   // 換成 GB
        if (d.ToString() == @"C:\")
        {
            driveCSpace = space;
            if (space < 10.0)
                enough = false;
        }
        if (d.ToString() == "D:\\")
        {
            driveDSpace = space;
            if (space < 10.0)
                enough = false;
        }
    }
}

(完)

沒有留言:

張貼留言