目录
0 基础理论
- 实际上字节并不是保存文件的最小单元。操作系统将硬盘划分为若干个簇,然后以簇作为最小单位保存文件。在FAT32中一簇是4KB,即使文件只有1个字节,在硬盘上也要占到4KB的空间 如果文件是4KB零1个字节,那就要占用8KB的空间,以此类推
- 每个簇的大小因为操作系统的分区大小而不同
- 大小是文件的实际大小,而占用空间是占硬盘的实际空间
1 获取文件或文件的大小
1.1 获取指定目录下所有文件的大小
//得到指定目录下的数据量大小
public static long GetDirectoryLength(string dirPath)
{
//判断给定的路径是否存在,如果不存在则退出
if (!Directory.Exists(dirPath))
return 0;
long len = 0;
//定义一个DirectoryInfo对象
DirectoryInfo di = new DirectoryInfo(dirPath);
//通过GetFiles方法,获取di目录中的所有文件的大小
foreach (FileInfo fi in di.GetFiles())
{
len += fi.Length;
}
//获取di中所有的文件夹,并存到一个新的对象数组中,以进行递归
DirectoryInfo[] dis = di.GetDirectories();
if (dis.Length > 0)
{
for (int i = 0; i < dis.Length; i++)
{
len += GetDirectoryLength(dis[i].FullName);
}
}
return len;
}
1.2 获取指定文件的大小
public static long GetFileLength(string filePath)
{
//判断当前路径所指向的是否为文件
if (File.Exists(filePath))
{
//定义一个FileInfo对象,使之与filePath所指向的文件向关联,
//以获取其大小
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Length;
}
else
{
return -1;
}
}
1.3 根据给定的目录或文件名获取其大小
//如果是目录记录目录的大小,如果是文件记录文件的大小
public static long GetDirectOrFileSize(string filePath)
{
//判断当前路径所指向的是否为文件
if (Directory.Exists(filePath) == true)
{
return GetDirectoryLength(filePath);
}
if(File.Exists(filePath) == true)
{
return GetFileLength(filePath);
}
return -1;
}
2 获取文件或目录的占用空间
2.1 得到磁盘信息
/// <summary>
/// 结构。硬盘信息
/// </summary>
public struct DiskInfo
{
public string RootPathName;
//每簇的扇区数
public int SectorsPerCluster;
//每扇区字节
public int BytesPerSector;
public int NumberOfFreeClusters;
public int TotalNumberOfClusters;
}
public static DiskInfo GetDiskInfo(string rootPathName)
{
DiskInfo diskInfo = new DiskInfo();
int sectorsPerCluster = 0, bytesPerSector = 0, numberOfFreeClusters = 0, totalNumberOfClusters = 0;
GetDiskFreeSpace(rootPathName, ref sectorsPerCluster, ref bytesPerSector, ref numberOfFreeClusters, ref totalNumberOfClusters);
//每簇的扇区数
diskInfo.SectorsPerCluster = sectorsPerCluster;
//每扇区字节
diskInfo.BytesPerSector = bytesPerSector;
diskInfo.NumberOfFreeClusters = numberOfFreeClusters;
diskInfo.TotalNumberOfClusters = totalNumberOfClusters;
diskInfo.RootPathName = rootPathName;
return diskInfo;
}
2.2 文件占用空间计算
/// <summary>
/// 获取每簇的字节
/// </summary>
/// <param name="dir">指定目录</param>
/// <returns></returns>
public static long GetClusterSize(DirectoryInfo dir)
{
long clusterSize = 0;
DiskInfo diskInfo = new DiskInfo();
diskInfo = GetDiskInfo(dir.Root.FullName);
clusterSize = (diskInfo.BytesPerSector * diskInfo.SectorsPerCluster);
return clusterSize;
}
//所给路径中所对应的文件占用空间
public static long FileSpace(string filePath)
{
long temp = 0;
//定义一个FileInfo对象,是指与filePath所指向的文件相关联,以获取其大小
FileInfo fileInfo = new FileInfo(filePath);
long clusterSize = GetClusterSize(fileInfo);
if (fileInfo.Length % clusterSize != 0)
{
decimal res = fileInfo.Length / clusterSize;
int clu = Convert.ToInt32(Math.Ceiling(res)) + 1;
temp = clusterSize * clu;//每簇的字节数 * 簇数
}
else
{
return fileInfo.Length;
}
return temp;
}
2.3 计算文件的占用空间
/// <summary>
/// 获取每簇的字节
/// </summary>
/// <param name="file">指定文件</param>
/// <returns></returns>
public static long GetClusterSize(FileInfo file)
{
long clusterSize = 0;
DiskInfo diskInfo = new DiskInfo();
diskInfo = GetDiskInfo(file.Directory.Root.FullName);
clusterSize = (diskInfo.BytesPerSector * diskInfo.SectorsPerCluster);
return clusterSize;
}
/// <summary>
/// 获取指定路径的占用空间
/// </summary>
/// <param name="dirPath">路径</param>
/// <returns></returns>
public static long GetDirectorySpace(string dirPath)
{
//返回值
long len = 0;
//判断该路径是否存在(是否为文件夹)
if (!Directory.Exists(dirPath))
{
//如果是文件,则调用
len = FileSpace(dirPath);
}
else
{
//定义一个DirectoryInfo对象
DirectoryInfo di = new DirectoryInfo(dirPath);
//本机的簇值
long clusterSize = GetClusterSize(di);
//遍历目录下的文件,获取总占用空间
foreach (FileInfo fi in di.GetFiles())
{
//文件大小除以簇,余若不为0
if (fi.Length % clusterSize != 0)
{
decimal res = fi.Length / clusterSize;
//文件大小除以簇,取整数加1。为该文件占用簇的值
int clu = Convert.ToInt32(Math.Ceiling(res)) + 1;
long result = clusterSize * clu;
len += result;
}
else
{
//余若为0,则占用空间等于文件大小
len += fi.Length;
}
}
//获取di中所有的文件夹,并存到一个新的对象数组中,以进行递归
DirectoryInfo[] dis = di.GetDirectories();
if (dis.Length > 0)
{
for (int i = 0; i < dis.Length; i++)
{
len += GetDirectorySpace(dis[i].FullName);
}
}
}
return len;
}
3 完整源码与运行结果
3.1 完整源码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;//获取文件信息
using System.Runtime.InteropServices;//导入dll
namespace ConsoleApplication2
{
class Program
{
//调用windows API获取磁盘空闲空间
//导入库
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern bool GetDiskFreeSpace([MarshalAs(UnmanagedType.LPTStr)]string rootPathName,
ref int sectorsPerCluster, ref int bytesPerSector, ref int numberOfFreeClusters, ref int totalNumbeOfClusters);
static void Main(string[] args)
{
Console.WriteLine("目录占用空间的计算:" + GetDirectorySpace(@"E:\C#\Example200\ConsoleApplication2\ConsoleApplication2").ToString());
Console.WriteLine("文件占用空间的计算:" + GetDirectorySpace(@"E:\C#\Example200\ConsoleApplication2\ConsoleApplication2\Program.cs").ToString());
Console.WriteLine("目录大小的计算:" + GetDirectoryLength(@"E:\C#\Example200\ConsoleApplication2\ConsoleApplication2").ToString());
Console.WriteLine("文件大小的计算:" + GetFileLength(@"E:\C#\Example200\ConsoleApplication2\ConsoleApplication2\Program.cs").ToString());
}
//得到指定目录下的数据量大小
public static long GetDirectoryLength(string dirPath)
{
//判断给定的路径是否存在,如果不存在则退出
if (!Directory.Exists(dirPath))
return -1;
long len = 0;
//定义一个DirectoryInfo对象
DirectoryInfo di = new DirectoryInfo(dirPath);
//通过GetFiles方法,获取di目录中的所有文件的大小
foreach (FileInfo fi in di.GetFiles())
{
len += fi.Length;
}
//获取di中所有的文件夹,并存到一个新的对象数组中,以进行递归
DirectoryInfo[] dis = di.GetDirectories();
if (dis.Length > 0)
{
for (int i = 0; i < dis.Length; i++)
{
len += GetDirectoryLength(dis[i].FullName);
}
}
return len;
}
public static long GetFileLength(string filePath)
{
//判断当前路径所指向的是否为文件
if (File.Exists(filePath))
{
//定义一个FileInfo对象,使之与filePath所指向的文件向关联,
//以获取其大小
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Length;
}
else
{
return -1;
}
}
//如果是目录记录目录的大小,如果是文件记录文件的大小
public static long GetDirectOrFileSize(string filePath)
{
//判断当前路径所指向的是否为文件
if (Directory.Exists(filePath) == true)
{
return GetDirectoryLength(filePath);
}
if (File.Exists(filePath) == true)
{
return GetFileLength(filePath);
}
return -1;
}
/// <summary>
/// 获取指定路径的占用空间
/// </summary>
/// <param name="dirPath">路径</param>
/// <returns></returns>
public static long GetDirectorySpace(string dirPath)
{
//返回值
long len = 0;
//判断该路径是否存在(是否为文件夹)
if (!Directory.Exists(dirPath))
{
//如果是文件,则调用
len = FileSpace(dirPath);
}
else
{
//定义一个DirectoryInfo对象
DirectoryInfo di = new DirectoryInfo(dirPath);
//本机的簇值
long clusterSize = GetClusterSize(di);
//遍历目录下的文件,获取总占用空间
foreach (FileInfo fi in di.GetFiles())
{
//文件大小除以簇,余若不为0
if (fi.Length % clusterSize != 0)
{
decimal res = fi.Length / clusterSize;
//文件大小除以簇,取整数加1。为该文件占用簇的值
int clu = Convert.ToInt32(Math.Ceiling(res)) + 1;
long result = clusterSize * clu;
len += result;
}
else
{
//余若为0,则占用空间等于文件大小
len += fi.Length;
}
}
//获取di中所有的文件夹,并存到一个新的对象数组中,以进行递归
DirectoryInfo[] dis = di.GetDirectories();
if (dis.Length > 0)
{
for (int i = 0; i < dis.Length; i++)
{
len += GetDirectorySpace(dis[i].FullName);
}
}
}
return len;
}
//所给路径中所对应的文件占用空间
public static long FileSpace(string filePath)
{
long temp = 0;
//定义一个FileInfo对象,是指与filePath所指向的文件相关联,以获取其大小
FileInfo fileInfo = new FileInfo(filePath);
long clusterSize = GetClusterSize(fileInfo);
if (fileInfo.Length % clusterSize != 0)
{
decimal res = fileInfo.Length / clusterSize;
int clu = Convert.ToInt32(Math.Ceiling(res)) + 1;
temp = clusterSize * clu;
}
else
{
return fileInfo.Length;
}
return temp;
}
/// <summary>
/// 结构。硬盘信息
/// </summary>
public struct DiskInfo
{
public string RootPathName;
//每簇的扇区数
public int SectorsPerCluster;
//每扇区字节
public int BytesPerSector;
public int NumberOfFreeClusters;
public int TotalNumberOfClusters;
}
public static DiskInfo GetDiskInfo(string rootPathName)
{
DiskInfo diskInfo = new DiskInfo();
int sectorsPerCluster = 0, bytesPerSector = 0, numberOfFreeClusters = 0, totalNumberOfClusters = 0;
GetDiskFreeSpace(rootPathName, ref sectorsPerCluster, ref bytesPerSector, ref numberOfFreeClusters, ref totalNumberOfClusters);
//每簇的扇区数
diskInfo.SectorsPerCluster = sectorsPerCluster;
//每扇区字节
diskInfo.BytesPerSector = bytesPerSector;
diskInfo.NumberOfFreeClusters = numberOfFreeClusters;
diskInfo.TotalNumberOfClusters = totalNumberOfClusters;
diskInfo.RootPathName = rootPathName;
return diskInfo;
}
/// <summary>
/// 获取每簇的字节
/// </summary>
/// <param name="file">指定文件</param>
/// <returns></returns>
public static long GetClusterSize(FileInfo file)
{
long clusterSize = 0;
DiskInfo diskInfo = new DiskInfo();
diskInfo = GetDiskInfo(file.Directory.Root.FullName);
clusterSize = (diskInfo.BytesPerSector * diskInfo.SectorsPerCluster);
return clusterSize;
}
/// <summary>
/// 获取每簇的字节
/// </summary>
/// <param name="dir">指定目录</param>
/// <returns></returns>
public static long GetClusterSize(DirectoryInfo dir)
{
long clusterSize = 0;
DiskInfo diskInfo = new DiskInfo();
diskInfo = GetDiskInfo(dir.Root.FullName);
clusterSize = (diskInfo.BytesPerSector * diskInfo.SectorsPerCluster);
return clusterSize;
}
}
}
3.2 结果展示