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. ArrayList和Iterator的用法

    import java.util.ArrayList; import java.util.Iterator; public class ArrayListTest { public static vo ...

  2. POJ 1279 Art Gallery(半平面交求多边形核的面积)

    题目链接 题意 : 求一个多边形的核的面积. 思路 : 半平面交求多边形的核,然后在求面积即可. #include <stdio.h> #include <string.h> ...

  3. Android Non-UI to UI Thread Communications(Part 1 of 5)

    original:http://www.intertech.com/Blog/android-non-ui-to-ui-thread-communications-part-1-of-5/ ANDRO ...

  4. Linux系统新手学习的11点建议

    随着Linux应用的扩展许多朋友开始接触Linux,根据学习Windwos的经验往往有一些茫然的感觉:不知从何处开始学起.这里介绍学习Linux的一些建议. 一.从基础开始:常常有些朋友在Linux论 ...

  5. hashmap理解总结

    1.hashmap是通过存放对象的hash算法进行对象的存储的,其查询,put,get方法均是. 所以将对象存储进hashmap,set啥时候,要重写hashcode方法. 2.hash算法存储,查询 ...

  6. uploadify IO Error/http error 413

    阿里云的服务器 linux 服务器,php 环境,上传附件问题,记录分享一下: 开始测试的时候,都是图片,小附件,没什么问题 系统上线后,发现大的附件上传 有问题,报错 IO Error 开始检查程序 ...

  7. 最近工作用到的sql脚本

    USE MadeInChina DUMP TRANSACTION MadeInChina WITH NO_LOG --清除日志 BACKUP LOG MadeInChina WITH NO_LOG B ...

  8. Hibernate常见错误整理

    Hibernate常见错误合集   1.错误:object references an unsaved transient instance - save the transient instance ...

  9. CentOS7区域设置

    区域设置的配置文件在/etc/locale.conf,通过localectl命令进行设置: systemd服务在启动的时候读取区域配置文件,完成系统的设置. 命令的几个常用方法如下: 1 查看当前配置 ...

  10. Oracle Demo ->> CREATE TABLE

    Demo One CREATE TABLE employees_demo ( employee_id ) , first_name ) , last_name ) CONSTRAINT emp_las ...