C#获取文件夹及文件的大小与占用空间的方法
生活随笔
收集整理的這篇文章主要介紹了
C#获取文件夹及文件的大小与占用空间的方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文詳細介紹了利用C#實現根據路徑,計算這個路徑所占用的磁盤空間的方法 。
網上有很多資料都是獲取文件夾/文件的大小的。對于占用空間的很少有完整的代碼。這里介紹實現這一功能的完整代碼,供大家參考一下。
首先說下文件夾/文件大小與占用空間的區別。
這個是硬盤分區格式有關 大小是文件的實際大小,而占用空間是占硬盤的實際空間 以FAT32格式為例,硬盤的基本存儲單位是簇,在FAT32中一簇是4KB 那么,也就是說即使文件只有1個字節,在硬盤上也要占到4KB的空間 如果文件是4KB零1個字節,那就要占用8KB的空間,以此類推。
結論:?大小是文件的實際大小,而占用空間是占硬盤的實際空間。
那么問題來了。怎樣獲取本機的簇有多少字節呢?
首先可以通過windows API獲取磁盤的相關信息。
//調用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);下面是具體代碼:
/// <summary> /// 獲取指定路徑的大小 /// </summary> /// <param name="dirPath">路徑</param> /// <returns></returns> public static long GetDirectoryLength(string dirPath) { long len = 0; //判斷該路徑是否存在(是否為文件夾) if (!Directory.Exists(dirPath)) { //查詢文件的大小 len = FileSize(dirPath); } else { //定義一個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; }/// <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 FileSize(string filePath) { //定義一個FileInfo對象,是指與filePath所指向的文件相關聯,以獲取其大小 FileInfo fileInfo = new FileInfo(filePath); return fileInfo.Length; }//所給路徑中所對應的文件占用空間 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; }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;return diskInfo; }//// <summary> /// 結構。硬盤信息 /// </summary> public struct DiskInfo { public string RootPathName; //每簇的扇區數 public int SectorsPerCluster; //每扇區字節 public int BytesPerSector; public int NumberOfFreeClusters; public int TotalNumberOfClusters; } /// <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; }?
總結
以上是生活随笔為你收集整理的C#获取文件夹及文件的大小与占用空间的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redhat server 5.3内核升
- 下一篇: CSS - 层叠特性