获取文件大小或文件夹的大小和占用空间,以及文件全路径、目录、扩展名、文件名称
获取文件或文件夹的大小
大小是文件夹/文件本身的实际大小,占用空间指的是文件夹/文件在磁盘中所占用的空间的大小。
-
获取指定目录下所有文件的大小
1 //得到指定目录下的数据量大小 2 public static long GetDirectoryLength(string dirPath) 3 { 4 //判断给定的路径是否存在,如果不存在则退出 5 if (!Directory.Exists(dirPath)) 6 return 0; 7 long len = 0; 8 9 //定义一个DirectoryInfo对象 10 DirectoryInfo di = new DirectoryInfo(dirPath); 11 12 //通过GetFiles方法,获取di目录中的所有文件的大小 13 foreach (FileInfo fi in di.GetFiles()) 14 { 15 len += fi.Length; 16 } 17 18 //获取di中所有的文件夹,并存到一个新的对象数组中,以进行递归 19 DirectoryInfo[] dis = di.GetDirectories(); 20 if (dis.Length > 0) 21 { 22 for (int i = 0; i < dis.Length; i++) 23 { 24 len += GetDirectoryLength(dis[i].FullName); 25 } 26 } 27 return len; 28 }
-
获取指定文件的大小
1 public static long GetFileLength(string filePath) 2 { 3 //判断当前路径所指向的是否为文件 4 if (File.Exists(filePath)) 5 { 6 //定义一个FileInfo对象,使之与filePath所指向的文件向关联, 7 //以获取其大小 8 FileInfo fileInfo = new FileInfo(filePath); 9 return fileInfo.Length; 10 } 11 else 12 { 13 return -1; 14 } 15 }
-
根据给定的目录或文件名获取其大小
1 //如果是目录记录目录的大小,如果是文件记录文件的大小 2 public static long GetDirectOrFileSize(string filePath) 3 { 4 //判断当前路径所指向的是否为文件 5 if (Directory.Exists(filePath) == true) 6 { 7 return GetDirectoryLength(filePath); 8 } 9 if(File.Exists(filePath) == true) 10 { 11 return GetFileLength(filePath); 12 } 13 return -1; 14 }
获取文件或目录的占用空间
-
得到磁盘信息
/// <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="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; }
-
计算文件的占用空间
1 2 3 /// <summary> 4 /// 获取每簇的字节 5 /// </summary> 6 /// <param name="file">指定文件</param> 7 /// <returns></returns> 8 public static long GetClusterSize(FileInfo file) 9 { 10 long clusterSize = 0; 11 DiskInfo diskInfo = new DiskInfo(); 12 diskInfo = GetDiskInfo(file.Directory.Root.FullName); 13 clusterSize = (diskInfo.BytesPerSector * diskInfo.SectorsPerCluster); 14 return clusterSize; 15 } 16 17 /// <summary> 18 /// 获取指定路径的占用空间 19 /// </summary> 20 /// <param name="dirPath">路径</param> 21 /// <returns></returns> 22 public static long GetDirectorySpace(string dirPath) 23 { 24 //返回值 25 long len = 0; 26 //判断该路径是否存在(是否为文件夹) 27 if (!Directory.Exists(dirPath)) 28 { 29 //如果是文件,则调用 30 len = FileSpace(dirPath); 31 } 32 else 33 { 34 //定义一个DirectoryInfo对象 35 DirectoryInfo di = new DirectoryInfo(dirPath); 36 //本机的簇值 37 long clusterSize = GetClusterSize(di); 38 //遍历目录下的文件,获取总占用空间 39 foreach (FileInfo fi in di.GetFiles()) 40 { 41 //文件大小除以簇,余若不为0 42 if (fi.Length % clusterSize != 0) 43 { 44 decimal res = fi.Length / clusterSize; 45 //文件大小除以簇,取整数加1。为该文件占用簇的值 46 int clu = Convert.ToInt32(Math.Ceiling(res)) + 1; 47 long result = clusterSize * clu; 48 len += result; 49 } 50 else 51 { 52 //余若为0,则占用空间等于文件大小 53 len += fi.Length; 54 } 55 } 56 //获取di中所有的文件夹,并存到一个新的对象数组中,以进行递归 57 DirectoryInfo[] dis = di.GetDirectories(); 58 if (dis.Length > 0) 59 { 60 for (int i = 0; i < dis.Length; i++) 61 { 62 len += GetDirectorySpace(dis[i].FullName); 63 } 64 } 65 } 66 return len; 67 }
C#路径中获取文件全路径,目录,扩展名,文件名称
使用Path的静态方法
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 //获取当前运行程序的目录 7 string fileDir = Environment.CurrentDirectory; 8 Console.WriteLine("当前程序目录:"+fileDir); 9 10 //一个文件目录 11 string filePath = "C:\\JiYF\\BenXH\\BenXHCMS.xml"; 12 Console.WriteLine("该文件的目录:"+filePath); 13 14 string str = "获取文件的全路径:" + Path.GetFullPath(filePath); //-->C:\JiYF\BenXH\BenXHCMS.xml 15 Console.WriteLine(str); 16 str = "获取文件所在的目录:" + Path.GetDirectoryName(filePath); //-->C:\JiYF\BenXH 17 Console.WriteLine(str); 18 str = "获取文件的名称含有后缀:" + Path.GetFileName(filePath); //-->BenXHCMS.xml 19 Console.WriteLine(str); 20 str = "获取文件的名称没有后缀:" + Path.GetFileNameWithoutExtension(filePath); //-->BenXHCMS 21 Console.WriteLine(str); 22 str = "获取路径的后缀扩展名称:" + Path.GetExtension(filePath); //-->.xml 23 Console.WriteLine(str); 24 str = "获取路径的根目录:" + Path.GetPathRoot(filePath); //-->C:\ 25 Console.WriteLine(str); 26 Console.ReadKey(); 27 28 } 29 }
获取目录下的目录以及所有文件
-
获取指定路径下的所有目录
1 /// <summary> 2 /// 递归得到子目录 3 /// </summary> 4 /// <param name="filePath">父级目录</param> 5 /// <param name="directoryList"></param> 6 /// <returns></returns> 7 private void GetDirectoryList(string filePath, List<DirectoryInfo> directoryList){ 8 //目录不存在 9 if (!Directory.Exists(filePath)) return ; 10 DirectoryInfo thisDirectory = new DirectoryInfo(filePath); 11 directoryList.Add(thisDirectory); 12 foreach(DirectoryInfo directory in thisDirectory.GetDirectories()){ 13 GetDirectoryList(directory.FullName, directoryList); 14 } 15 }
-
得到指定目录下的所有文件
1 /// <summary> 2 /// 得到指定目录下的文件 3 /// </summary> 4 /// <param name="filePath"></param> 5 /// <returns></returns> 6 private List<FileInfo> GetFileList(string filePath){ 7 List<FileInfo> fileList = new List<FileInfo>(); 8 //目录不存在 9 if (!Directory.Exists(filePath)) return fileList; 10 List<DirectoryInfo> directoryList = new List<DirectoryInfo>(); 11 //得到所有子孙目录 12 GetDirectoryList(filePath, directoryList); 13 //得到所有文件 14 foreach(DirectoryInfo directory in directoryList){ 15 foreach (FileInfo file in directory.GetFiles()){ 16 fileList.Add(file); 17 } 18 } 19 return fileList; 20 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!