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

  • 相关阅读:
    exit() _exit() 函数区别
    Linux-进程间通信(五): 网络套接字
    Linux-进程间通信(六): 记录锁
    Linux-进程间通信(N): 各种IPC的使用场景
    SQL EXISTS
    日志格式化
    logminer实战之生产环境写入数据字典,dg环境查询拷贝日志,测试环境进行挖掘,输出结果
    awr脚本使用dump导出导入
    logminer使用测试库进行挖掘分析,10.2.0.5
    10.2.0.5环境dg测试logminer挖掘日志分析
  • 原文地址:https://www.cnblogs.com/sskset/p/3580620.html
Copyright © 2011-2022 走看看