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 ...
随机推荐
- ZOJ2923 Calculate Roads(SPFA上的dp)
算是学了图dp后的第一次应用吧.题目其实真的是非常不严谨,什么都没说,基本靠猜,而且严格来说数据应该会有爆int的,不过不管那么多啦,思路对了就好- -0 #include<iostream&g ...
- oc和swift的混编
参考:http://blog.sina.com.cn/s/blog_8d1bc23f0102v5tl.html swift中使用oc类的方法 1.创建一个oc.h文件 2.添加需要倒入的oc类的头文件 ...
- HDU 1421 搬寝室
搬寝室 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- 关闭WordPress自动加载的Open Sans字体-WP访问过慢原因
序言 wordpress大概从wp-3.8开始会自动加载Open Sans字体,并引用Google上面的CSS样式.而最近谷歌经常打不开,导致网站访问速度过慢,严重的会拖慢几十秒.Open Sans字 ...
- CXF+Spring 搭建的WebService
1.创建类 2.接口编写 package com.fan; import javax.jws.WebService; @WebService public interface IHelloWorld ...
- UVA 10341 二分搜索
Solve the equation:p ∗ e−x + q ∗ sin(x) + r ∗ cos(x) + s ∗ tan(x) + t ∗ x2 + u = 0where 0 ≤ x ≤ 1.In ...
- 入门命令13-字符串查找增强:findstr
http://www.cnblogs.com/doupip/archive/2011/12/23/2299210.html 在文件中寻找字符串. FINDSTR [/B] [/E] [/L] [/R] ...
- TCP非阻塞accept和非阻塞connect
http://blog.chinaunix.net/uid-20751538-id-238260.html 非阻塞accept 当一个已完成的连接准备好被accept的时候,select会把监 ...
- qt中如何启动其他应用程序(如果不成功,还有许多原因即QProcess::ProcessError可供分析)
类 QDesktopServices 提供的方法 访问 常用的桌面 服务 , 如 浏览 器 . 播放器. 电子邮件客户端 . 我们 使用 QDesktopServices :: openUrl(url ...
- Redis的List操作
lpush key value 作用: 把值插入到链接头部 127.0.0.1:6379> lpush character a (integer) 1 127.0.0.1:6379> rp ...