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 Web.Config配置数据库连接的一种方法

    所谓的webConfig配置数据库连接就是在里面某个特定名称的节点中写下ADP.NET中的ConnectString,就这么简单 1.首先在Web.Config文件里写下数据库连接字符串. <c ...

  2. Android线程消息通信(一)

    Android在Java标准线程模型的基础上,提供了消息驱动机制,用于多线程之间的通信.基于消息驱动机制的线程通信模型陈伟线程消息通信.在标准线程模型中,线程执行完毕后便退出,而Android扩展了线 ...

  3. android开发环境搭建(for 驱动开发人员)

    前言 一.android驱动的开发流程 1: 写LINUX驱动 2: 写LINUX应用测试程序 3: 写JNI接口,用来包装第二步写的应用 (要用NDK来编译) 生成一个.SO文件,相当于CE下的DL ...

  4. POJ 3130 How I Mathematician Wonder What You Are!(半平面交求多边形的核)

    题目链接 题意 : 给你一个多边形,问你该多边形中是否存在一个点使得该点与该多边形任意一点的连线都在多边形之内. 思路 : 与3335一样,不过要注意方向变化一下. #include <stdi ...

  5. ExtJs之Ext.form.field.ComboBox组合框

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

  6. Google Protocol Buffers简介

    什么是 protocol buffers ? Protocol buffers 是一种灵活.高效的序列化结构数据的自动机制--想想XML,但是它更小,更快,更简单.你只需要把你需要怎样结构化你的数据定 ...

  7. java 哪些情况下会使对象锁释放

    Java_多线程_锁释放 问:Java多线程运行环境中,在哪些情况下会使对象锁释放?答:由于等待一个锁的线程只有在获得这把锁之后,才能恢复运行,所以让持有锁的线程在不再需要锁的时候及时释放锁是很重要的 ...

  8. 使用内网的Docker Image启动AcmeAir应用

    1.修改docker启动选项,添加信赖私有仓库 为了拉取docker.oneapm.me仓库里的镜像,我们需要将它添加为信赖仓库,方式是在启动选项中添加 --insecure-registry doc ...

  9. C#服务启动以及服务指令

    Windows系统中使用 cmd 命令安装和卸载服务方法如下: 第一种方法: 1. 开始->运行->cmd 2. cd到C:\WINDOWS\Microsoft.NET\Framework ...

  10. 使用 .gitignore来忽略某些文件【转】

    转自:http://www.cnblogs.com/shangdawei/archive/2012/09/08/2676493.htmlhttp://blog.csdn.net/richardyste ...