c# 递归遍历选择文件夹下所有文件并计算该类型大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace FileRetrieval { public static class FileUtils { public static void Director( string dir) { DirectoryInfo d = new DirectoryInfo(dir); FileSystemInfo[] fsinfos = d.GetFileSystemInfos(); foreach (FileSystemInfo fsinfo in fsinfos) { if (fsinfo is DirectoryInfo) //判断是否为文件夹 { Director(fsinfo.FullName); //递归调用 } else { Console.WriteLine(fsinfo.FullName); //输出文件的全部路径 } } } // 测试一下 public static void TestGetCurrentFileList( string dataPath, Dictionary< string , string > configs) { StringBuilder stringBuilder = new StringBuilder(); for ( var i = 0; i < configs.Count; i++) { var item = configs.ElementAt(i); var con1 = item.Key; var con2 = item.Value; var conArray = con2.Split( ',' ); stringBuilder.AppendLine( string .Format( "[{0}]" , con1)); foreach ( var a in conArray) { var fs = GetFileList(dataPath, names: a); long size = 0; foreach ( var f in fs) { size += GetFileLength(f.FullName); //stringBuilder.AppendLine($"文件名:{f.FullName},大小:{ GetFileSize(GetFileLength(f.FullName))}"); } stringBuilder.AppendLine($ "(扩展名:{a},数量:{fs.Count},大小:{GetFileSize(size)})" ); } } WriteData(stringBuilder.ToString()); } private static long GetFileLength( string filePath) { FileInfo t = new FileInfo(filePath); //获取文件 var size = t.Length; //这样我们就获取到了文件的大小 return size; } /// <summary> /// 格式化文件大小 /// </summary> /// <param name="filesize">文件传入大小</param> /// <returns></returns> private static string GetFileSize( long filesize) { try { if (filesize < 0) { return "0" ; } else if (filesize >= 1024 * 1024 * 1024) //文件大小大于或等于1024MB { return string .Format( "{0:0.00} GB" , ( double )filesize / (1024 * 1024 * 1024)); } else if (filesize >= 1024 * 1024) //文件大小大于或等于1024KB { return string .Format( "{0:0.00} MB" , ( double )filesize / (1024 * 1024)); } else if (filesize >= 1024) //文件大小大于等于1024bytes { return string .Format( "{0:0.00} KB" , ( double )filesize / 1024); } else { return string .Format( "{0:0.00} bytes" , filesize); } } catch (Exception ex) { throw ex; } } private static void WriteData( string str) { string path = System.AppDomain.CurrentDomain.BaseDirectory + "Info.txt" ; using (StreamWriter sw = new StreamWriter(path, false , Encoding.UTF8)) { sw.Write(str); } } /// <summary> /// 递归遍历文件夹获取文件 /// </summary> /// <param name="path"></param> public static List<FileInfo> GetFileList( string path, bool isRecursion = true , int pathDeep = 20, List<FileInfo> fileInfos = null , params string [] names) { if (fileInfos == null ) { fileInfos = new List<FileInfo>(); } if (pathDeep < 0) { return fileInfos; } DirectoryInfo theFolder = new DirectoryInfo(path); //获取文件夹下文件 FileInfo[] files = theFolder.GetFiles(); foreach ( var file in files) { foreach ( var fn in names) { if ( string .IsNullOrEmpty(fn)) { continue ; } if (file.Name.ToLower() == fn.ToLower()) { fileInfos.Add(file); } } } if (isRecursion) { // 如果是递归,遍历该文件夹下的文件夹 var dirs = theFolder.GetDirectories(); if (!dirs.Any()) { return fileInfos; } foreach ( var dir in dirs) { GetFileList(dir.FullName, isRecursion, --pathDeep, fileInfos, names); } } return fileInfos; } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~