zoukankan      html  css  js  c++  java
  • Calculate drive total/free/available space

    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;
            }
        }
    }

  • 相关阅读:
    Ubuntu 10.04安装google拼音输入法
    Ubuntu 10.04 编译Android 2.1源码
    Android make sdk 错误解决方案
    关于android内核从linux内核分支上除名
    odex打包为可用的apk程序
    取得当前屏幕的截图
    android设备作为视频监控客户端的思路
    政府网站群系统选型
    浅谈网站群的一代与二代技术
    我的2013
  • 原文地址:https://www.cnblogs.com/sskset/p/3580620.html
Copyright © 2011-2022 走看看