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. Rake::TestTask 介绍

    Rake::TestTask 介绍 通常我们创建一个新的项目的时候,会建立一个test或者spec的文件夹来存放测试的文件,运行这些测试需要单独的命令,比如在项目目录下执行rspec .或者ruby ...

  2. Python下载Yahoo!Finance数据

    Python下载Yahoo!Finance数据的三种工具: (1)yahoo-finance package. (2)ystockquote. (3)pandas.

  3. javascript console

    javascript console console.log(object[, object, ...])在控制台输出一条消息.如果有多个参数,输出时会用空格隔开这些参数. 第一个参数可以是一个包含格 ...

  4. 李洪强漫谈iOS开发[C语言-004]-开发概述程序设计语言程序编译过程

    汇编语言 指令用特定的名字来标记,这就是汇编语言 人比较容易看懂汇编语言 汇编直接和程序一一对应的 有汇编器把程序翻译成机器码 把高级语言编译成计算机识别的语言 程序编译过程 命令行 UNIX 系统中 ...

  5. (转载)怎么写tab?

    演示地址:http://www.adanghome.com/js_demo/3/ =========================================================== ...

  6. nodejs 操作mysql

    这篇文章主要介绍了nodejs中操作mysql数据库示例,本文演示了如何在NodeJS中创建创建mysql连接.mysql数据库.插入数据.查询数据等功能,需要的朋友可以参考下  引言: 继前面的No ...

  7. Heap和Heapify

    最近复习数据结构,又回去再看塞神的课件,看到PriorityQueue的实现.自己也根据塞神的代码写一写. 下面使用Binary Heap实现了一个简单的 Max-oriented PriorityQ ...

  8. Spring_MVC_教程_快速入门_深入分析

    Spring MVC 教程,快速入门,深入分析 博客分类: SPRING Spring MVC 教程快速入门  资源下载: Spring_MVC_教程_快速入门_深入分析V1.1.pdf Spring ...

  9. R语言---热图的制作

    >install.packages("gplots") > library("gplots")> p <- data.frame(rea ...

  10. USACO Section 3.3: A Game

    第一次碰到博弈论题目,是很棘手,博弈论题目要考虑全局最优的解法,我第一次用了局部最优的,而且vector也没pop_front()操作.后来看了网上的用dp的方法解的. 博弈论的题目基本都得用dp法子 ...