本文詳細(xì)介紹了利用C#實(shí)現(xiàn)根據(jù)路徑,計(jì)算這個(gè)路徑所占用的磁盤空間的方法 。
網(wǎng)上有很多資料都是獲取文件夾/文件的大小的。對于占用空間的很少有完整的代碼。這里介紹實(shí)現(xiàn)這一功能的完整代碼,供大家參考一下。
首先說下文件夾/文件大小與占用空間的區(qū)別。
這個(gè)是硬盤分區(qū)格式有關(guān) 大小是文件的實(shí)際大小,而占用空間是占硬盤的實(shí)際空間 以FAT32格式為例,硬盤的基本存儲(chǔ)單位是簇,在FAT32中一簇是4KB 那么,也就是說即使文件只有1個(gè)字節(jié),在硬盤上也要占到4KB的空間 如果文件是4KB零1個(gè)字節(jié),那就要占用8KB的空間,以此類推。
結(jié)論: 大小是文件的實(shí)際大小,而占用空間是占硬盤的實(shí)際空間。
那么問題來了。怎樣獲取本機(jī)的簇有多少字節(jié)呢?
首先可以通過windows API獲取磁盤的相關(guān)信息。
//調(diào)用windows API獲取磁盤空閑空間//導(dǎo)入庫[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{//定義一個(gè)DirectoryInfo對象DirectoryInfo di = new DirectoryInfo(dirPath);//通過GetFiles方法,獲取di目錄中的所有文件的大小foreach (FileInfo fi in di.GetFiles()){len += fi.Length;}//獲取di中所有的文件夾,并存到一個(gè)新的對象數(shù)組中,以進(jìn)行遞歸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)){//如果是文件,則調(diào)用len = FileSpace(dirPath);}else{//定義一個(gè)DirectoryInfo對象DirectoryInfo di = new DirectoryInfo(dirPath);//本機(jī)的簇值long clusterSize = GetClusterSize(di);//遍歷目錄下的文件,獲取總占用空間foreach (FileInfo fi in di.GetFiles()){//文件大小除以簇,余若不為0if (fi.Length % clusterSize != 0){decimal res = fi.Length / clusterSize;//文件大小除以簇,取整數(shù)加1。為該文件占用簇的值int clu = Convert.ToInt32(Math.Ceiling(res)) + 1;long result = clusterSize * clu;len += result;}else{//余若為0,則占用空間等于文件大小len += fi.Length;}}//獲取di中所有的文件夾,并存到一個(gè)新的對象數(shù)組中,以進(jìn)行遞歸DirectoryInfo[] dis = di.GetDirectories();if (dis.Length > 0){for (int i = 0; i < dis.Length; i++){len += GetDirectorySpace(dis[i].FullName);}}}return len;}//所給路徑中所對應(yīng)的文件大小public static long FileSize(string filePath){//定義一個(gè)FileInfo對象,是指與filePath所指向的文件相關(guān)聯(lián),以獲取其大小FileInfo fileInfo = new FileInfo(filePath);return fileInfo.Length;}//所給路徑中所對應(yīng)的文件占用空間public static long FileSpace(string filePath){long temp = 0;//定義一個(gè)FileInfo對象,是指與filePath所指向的文件相關(guān)聯(lián),以獲取其大小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);//每簇的扇區(qū)數(shù)diskInfo.SectorsPerCluster = sectorsPerCluster;//每扇區(qū)字節(jié)diskInfo.BytesPerSector = bytesPerSector;return diskInfo;}//// <summary>/// 結(jié)構(gòu)。硬盤信息/// </summary>public struct DiskInfo{public string RootPathName;//每簇的扇區(qū)數(shù)public int SectorsPerCluster;//每扇區(qū)字節(jié)public int BytesPerSector;public int NumberOfFreeClusters;public int TotalNumberOfClusters;}/// <summary>/// 獲取每簇的字節(jié)/// </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>/// 獲取每簇的字節(jié)/// </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;}
新聞熱點(diǎn)
疑難解答
圖片精選