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;
}
}
}
Calculate drive total/free/available space的更多相关文章
- Disk Space Usage 术语理解:unallocated, unused and reserved
通过standard reports查看Disk Usage,选中Database,右击,选择Reports->Standard Reports->Disk Space Usage,截图如 ...
- 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 ...
- ZOJ 2476 Total Amount
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2476 Time Limit: 2 Seconds ...
- ZOJ 2476 Total Amount 字符串模拟
- Total Amount Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- psutil官方文档
psutil documentation¶ Quick links Home page Install Blog Forum Download Development guide What’s new ...
- 【OS】NMON的简介和使用
[OS]NMON的简介和使用 目前NMON已开源,以sourceforge为根据地,网址是http://nmon.sourceforge.net. 1. 目的 本文介绍操作系统监控工具Nmon的概念. ...
- 监控系统信息模块psutil
About psutil (python system and process utilities) is a cross-platform library for retrieving inform ...
- 非常全面的SQL Server巡检脚本来自sqlskills团队的Glenn Berry 大牛
非常全面的SQL Server巡检脚本来自sqlskills团队的Glenn Berry 大牛 Glenn Berry 大牛会对这个脚本持续更新 -- SQL Server 2012 Diagnost ...
- SQL Server 诊断查询-(2)
Query #13 SQL Server Error Log(FC) -- Shows you where the SQL Server failover cluster diagnostic log ...
随机推荐
- 在 Java EE 组件中使用 Camel Routes
摘要:你可以通过集成 Camel 和 WildFly 应用服务器(使用 WildFly-Camel 子系统)在 Java EE 组件中开始使用 Apache Camel Routes. [编者按]作者 ...
- HDU 3998 Sequence (最长上升子序列+最大流)
参考链接:http://www.cnblogs.com/gentleh/archive/2013/03/30/2989958.html 题意:求一个序列的最长上升子序列,及其个数(注意:两个最长上升子 ...
- SIOCADDRT: No such process
配置Ubuntu靶机遇到的问题 如果你添加/修改默认网关时遇到这个问题. 原因:你要添加的网关不在你主机所在的网段. 解决方法: 比如你要添加的网关是10.57.50.1 sudo route add ...
- 李洪强iOS开发之OC面向对象—多态
OC面向对象—多态 一.基本概念 多态在代码中的体现,即为多种形态,必须要有继承,没有继承就没有多态. 在使用多态是,会进行动态检测,以调用真实的对象方法. 多态在代码中的体现即父类指针指向子类对象. ...
- android listview 加载图片错乱(错位)
写道 今天晚上一个朋友介绍我看了一篇文章,也是解决android中listview在加载图片错位的问题,看了之后,感觉写的很好,自己也遇到这个问题,但是又不知道从何下手,看到这篇文章后,我的问题 ...
- Keil MDK编译器(V4.03)与J-LINK使用
前几天进手了一个J-LINK,因为H-JTAG毕竟对MDK支持的不是太完美,比如用keil mdk上面的下载按钮烧录程序,不是直接就能烧录进去,而是弹出H-Flash软件,再手工选择文件烧录:而且用H ...
- ArcGIS Runtime SDK for Android 10.2.5新开发平台安装配置指南
ArcGIS Runtime SDK for Android 10.2.5版本在年前发布,其中一个重大的变化是:新版本使用了新的开发环境,在10.2.5版本中Esri使用了官方提供的新的Android ...
- Open_Newtonsoft_Json 的序列化和反序列化
Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库(下载地址http://json.codeplex.com/). 特别注明:本人转自 陈 晨 博客园的 Newtonso ...
- 【HDOJ】1362 The Bermuda Triangle
1. 题目描述给定几个三角形拼成一个百慕大三角形. 2. 基本思路基本思路肯定是搜索,关键点是剪枝.(1) 若存在长度为$l$的边,则一定可以拼成长度为$k \cdot l$的三角形,则可拼成长度为$ ...
- [HDOJ4325]Flowers(树状数组 离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 关于离散化的简介:http://blog.csdn.net/gokou_ruri/article ...