国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C#獲取文件夾及文件的大小與占用空間的方法

2020-01-24 02:39:07
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文詳細(xì)介紹了利用C#實(shí)現(xiàn)根據(jù)路徑,計(jì)算這個(gè)路徑所占用的磁盤(pán)空間的方法 。

網(wǎng)上有很多資料都是獲取文件夾/文件的大小的。對(duì)于占用空間的很少有完整的代碼。這里介紹實(shí)現(xiàn)這一功能的完整代碼,供大家參考一下。

首先說(shuō)下文件夾/文件大小與占用空間的區(qū)別。

這個(gè)是硬盤(pán)分區(qū)格式有關(guān) 大小是文件的實(shí)際大小,而占用空間是占硬盤(pán)的實(shí)際空間 以FAT32格式為例,硬盤(pán)的基本存儲(chǔ)單位是簇,在FAT32中一簇是4KB 那么,也就是說(shuō)即使文件只有1個(gè)字節(jié),在硬盤(pán)上也要占到4KB的空間 如果文件是4KB零1個(gè)字節(jié),那就要占用8KB的空間,以此類(lèi)推

結(jié)論: 大小是文件的實(shí)際大小,而占用空間是占硬盤(pán)的實(shí)際空間

那么問(wèn)題來(lái)了。怎樣獲取本機(jī)的簇有多少字節(jié)呢?

首先可以通過(guò)windows API獲取磁盤(pán)的相關(guān)信息。

//調(diào)用windows API獲取磁盤(pán)空閑空間//導(dǎo)入庫(kù)[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)){//查詢(xún)文件的大小len = FileSize(dirPath);}else{//定義一個(gè)DirectoryInfo對(duì)象DirectoryInfo di = new DirectoryInfo(dirPath);//通過(guò)GetFiles方法,獲取di目錄中的所有文件的大小foreach (FileInfo fi in di.GetFiles()){len += fi.Length;}//獲取di中所有的文件夾,并存到一個(gè)新的對(duì)象數(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對(duì)象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è)新的對(duì)象數(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;}//所給路徑中所對(duì)應(yīng)的文件大小public static long FileSize(string filePath){//定義一個(gè)FileInfo對(duì)象,是指與filePath所指向的文件相關(guān)聯(lián),以獲取其大小FileInfo fileInfo = new FileInfo(filePath);return fileInfo.Length;}//所給路徑中所對(duì)應(yīng)的文件占用空間public static long FileSpace(string filePath){long temp = 0;//定義一個(gè)FileInfo對(duì)象,是指與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)。硬盤(pán)信息/// </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;}
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 连江县| 廉江市| 福安市| 柘城县| 五莲县| 大荔县| 武义县| 房山区| 四平市| 凌云县| 黄大仙区| 会泽县| 玛多县| 韶关市| 康平县| 鄯善县| 通江县| 上虞市| 宜兰县| 宿迁市| 陇西县| 拜泉县| 洛川县| 江西省| 襄城县| 普格县| 南木林县| 长兴县| 土默特左旗| 红安县| 彭山县| 酒泉市| 和田县| 汝城县| 莱芜市| 兴义市| 印江| 浑源县| 定陶县| 甘洛县| 富平县|