惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
Last Week in AI
Last Week in AI
Blog — PlanetScale
Blog — PlanetScale
V
Visual Studio Blog
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
博客园 - Franky
IT之家
IT之家
博客园 - 叶小钗
Engineering at Meta
Engineering at Meta
The GitHub Blog
The GitHub Blog
雷峰网
雷峰网
腾讯CDC
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
人人都是产品经理
人人都是产品经理
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
博客园_首页
G
Google Developers Blog

博客园 - 彭旭

VNC远程Gnome图形桌面(openEuler 20.03 LTS系统) [WPF]控件应用多个样式 [ASP.NET MVC] Real-time之HTML5 服务器发送事件(server-sent event) [WCF]DomainServices客户端操作异常处理 [Silverlight]监听指定控件(FrameworkElement)的依赖属性(DependencyProperty)的更改 [Silverlight]键盘钩子KeyboardHook [Chrome扩展]屏蔽Google搜索结果重定向 Linq Distinct扩展 [Winform/C#]点击工作区域移动窗体 [WinForm] C#避免Panel(ScrollableControl)的子控件获取焦点时滚动条自动滚动 C#远程连接Oracle数据库(不安装客户端) RichTextBox.MouseWheel事件控制父控件Panel的内容滚动 ID3v1信息结构(MP3文件)的读取、修改(C#) 在线编辑Office文件 SQL优化 将自然数分解为若干连续的自然数 小写金额转换为大写金额 大数阶乘 VS2005开发WAP网站初识 - 彭旭 - 博客园
查看文件夹大小及占用空间
彭旭 · 2010-01-06 · via 博客园 - 彭旭

准备

  1.簇:要了解文件的大小及占用空间的含义,必须先了解簇的定义。相关信息请看这里

  2.GetDiskFreeSpace:获取指定磁盘的信息,包括磁盘的可用空间(Retrieves information about the specified disk, including the amount of free space on the disk)。参考这里。此方法来自 kernel32.dll。具体调用请看正文。

正文

  1.调用GetDiskFreeSpace获取磁盘信息。

/// <summary>
/// Retrieves information about the specified disk, including the amount of free space on the disk.
/// 获取指定磁盘的信息,包括磁盘的可用空间。
/// </summary>
/// <param name="lpRootPathName">磁盘根目录。如:"C:\\"</param>
/// <param name="lpSectorsPerCluster">每个簇所包含的扇区个数</param>
/// <param name="lpBytesPerSector">每个扇区所占的字节数</param>
/// <param name="lpNumberOfFreeClusters">空闲的簇的个数</param>
/// <param name="lpTotalNumberOfClusters">簇的总个数</param>
/// <returns></returns>
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool GetDiskFreeSpace(string lpRootPathName,
out uint lpSectorsPerCluster,
out uint lpBytesPerSector,
out uint lpNumberOfFreeClusters,
out uint lpTotalNumberOfClusters);

  2.获取指定磁盘的簇大小。

public int BytesPerCluster(string drive)
{
uint lpSectorsPerCluster;
uint lpBytesPerSector;
uint lpNumberOfFreeClusters;
uint lpTotalNumberOfClusters;
GetDiskFreeSpace(drive,
out lpSectorsPerCluster,
out lpBytesPerSector,
out lpNumberOfFreeClusters,
out lpTotalNumberOfClusters);
return (int)(lpBytesPerSector * lpSectorsPerCluster);
}

  3.获取文件夹大小及占用空间。

public void DirectoryProperities(string path, int bytesPerCluster, ref long size, ref long usage)
{
long temp;
FileInfo fi = null;
foreach (string file in Directory.GetFiles(path, "*", SearchOption.AllDirectories))
{
fi = new FileInfo(file);
temp = fi.Length;
size += temp;
usage += temp + (temp % bytesPerCluster == 0 ? 0 : bytesPerCluster - temp % bytesPerCluster);
}
}

  4.剩下的就是除法运算了。

private void Form1_Load(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
string path = folderBrowserDialog1.SelectedPath;
long size = 0, usage = 0;
DirectoryProperities(path, BytesPerCluster(Path.GetPathRoot(path)), ref size, ref usage);
lblSize.Text = ((double)size / 1024 / 1024).ToString("0.00 MB")
+ "(" + size.ToString("N0") + " 字节)";
lblUsage.Text = ((double)(usage >> 10) / 1024).ToString("0.00 MB")
+ "(" + usage.ToString("N0") + " 字节)";
}
}

  5.大功告成。