using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            long? systemTotalSpace = GetDriveTotalSpace(Path.GetPathRoot(Environment.SystemDirectory));
            long? systemAvailableSpace = GetDriveFreeSpace(Path.GetPathRoot(Environment.SystemDirectory));
            long? remoteAvailableSpace = GetDriveOccupiedSpace(Path.GetPathRoot(Environment.SystemDirectory));

            if(systemTotalSpace.HasValue)
                Console.WriteLine("C: total space " + ConvertBytesToGString(systemTotalSpace.Value));

            if (systemAvailableSpace.HasValue)
                Console.WriteLine("C: free space " + ConvertBytesToGString(systemAvailableSpace.Value));

            if (remoteAvailableSpace.HasValue)
                Console.WriteLine("C: occupied space " + ConvertBytesToGString(remoteAvailableSpace.Value));

            Console.ReadKey();
        }

        static string ConvertBytesToGString(long size)
        {
            string[] Suffix = { "b", "K", "M", "G", "T" };
            const long Unit = 1024;

            int sIndex = 0;
            decimal dSize = new decimal(size);

            while (dSize > Unit)
            {
                dSize = dSize / Unit;

                sIndex++;
            }

            return string.Format("{0} {1} ", Math.Round(dSize, 2), Suffix[sIndex]);
        }

        static long? GetDriveOccupiedSpace(string driveName)
        {
            DriveInfo[] allDrives = DriveInfo.GetDrives();

            foreach (DriveInfo d in allDrives)
            {
                if (d.Name.Equals(driveName, StringComparison.OrdinalIgnoreCase))
                {
                    return d.TotalSize - d.TotalFreeSpace;
                }
            }

            return null;
        }
        static long? GetDriveTotalSpace(string driveName)
        {
            DriveInfo[] allDrives = DriveInfo.GetDrives();

            foreach (DriveInfo d in allDrives)
            {
                if (d.Name.Equals(driveName, StringComparison.OrdinalIgnoreCase))
                {
                    return d.TotalSize;
                }
            }

            return null;
        }

        static long? GetDriveFreeSpace(string driveName)
        {
            DriveInfo[] allDrives = DriveInfo.GetDrives();

            foreach (DriveInfo d in allDrives)
            {
                if (d.Name.Equals(driveName, StringComparison.OrdinalIgnoreCase))
                {
                    return d.AvailableFreeSpace; //Available free space for current user
                }
            }

            return null;
        }
    }
}

Calculate drive total/free/available space的更多相关文章

  1. Disk Space Usage 术语理解:unallocated, unused and reserved

    通过standard reports查看Disk Usage,选中Database,右击,选择Reports->Standard Reports->Disk Space Usage,截图如 ...

  2. How to Calculate difference between two dates in C# z

    Do you need to find the difference in number of days, hours or even minute between the two date rang ...

  3. ZOJ 2476 Total Amount

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2476 Time Limit: 2 Seconds         ...

  4. ZOJ 2476 Total Amount 字符串模拟

    - Total Amount Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit ...

  5. psutil官方文档

    psutil documentation¶ Quick links Home page Install Blog Forum Download Development guide What’s new ...

  6. 【OS】NMON的简介和使用

    [OS]NMON的简介和使用 目前NMON已开源,以sourceforge为根据地,网址是http://nmon.sourceforge.net. 1. 目的 本文介绍操作系统监控工具Nmon的概念. ...

  7. 监控系统信息模块psutil

    About psutil (python system and process utilities) is a cross-platform library for retrieving inform ...

  8. 非常全面的SQL Server巡检脚本来自sqlskills团队的Glenn Berry 大牛

    非常全面的SQL Server巡检脚本来自sqlskills团队的Glenn Berry 大牛 Glenn Berry 大牛会对这个脚本持续更新 -- SQL Server 2012 Diagnost ...

  9. SQL Server 诊断查询-(2)

    Query #13 SQL Server Error Log(FC) -- Shows you where the SQL Server failover cluster diagnostic log ...

随机推荐

  1. ASP.NET Excel 导入 Oracle 方法2

    先谈思路:前半部分和之前那篇日志的内容是一样的,把Excel数据导入到DataSet中,不同之处在于数据插入的方式: 本方法是拼接 INSERT INTO TABLE VALUES() 字符串,对,就 ...

  2. javascript利用拷贝的方法实现导出excel(可以导出表格线)

    Js代码: <script language=javascript> function preview() { window.clipboardData.setData("Tex ...

  3. POJ 1466

    #include<iostream> #include<stdio.h> #define MAXN 505 using namespace std; int edge[MAXN ...

  4. Spinner学习

    我暂且把Spinner称作下拉选择框吧,先来看一下它的效果: 在layout文件中添加Spinner的代码如下: <Spinner android:id="@+id/planets_s ...

  5. sql server 数据库 数据DateTime 转mysql

    首先将sql server  DateTime 转换为varchar(50) 然后更新转换过的 DateTime字段, UPDATE 表名 SET LastUpdateTime=CONVERT(VAR ...

  6. ExtJs之Ext.util.ClickRepeater

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  7. ArrayList,LinkedList,Vector,Stack之间的区别

    一,线程安全性 Vector.Stack:线程安全 ArrayList.LinkedList:非线程安全 二,实现方式 LinkedList:双向链表 ArrayList,Vector,Stack:数 ...

  8. lintcode :最长公共前缀

    题目 最长公共前缀 给k个字符串,求出他们的最长公共前缀(LCP) 样例 在 "ABCD" "ABEF" 和 "ACEF" 中,  LCP ...

  9. P2P通信标准协议(一)之STUN

    前一段时间在P2P通信原理与实现中介绍了P2P打洞的基本原理和方法,我们可以根据其原理为自己的网络程序设计一套通信规则, 当然如果这套程序只有自己在使用是没什么问题的.可是在现实生活中,我们的程序往往 ...

  10. 批处理命令 - if

    0.功能 Performs conditional processing in batch programs. 执行批处理程序中的条件处理. 1.简介 IF [NOT] ERRORLEVEL numb ...