文件操作类FileUtil
1 public class FileUtil 2 { 3 /// <summary> 4 /// 按时间来创建文件夹 5 /// </summary> 6 /// <param name="path"></param> 7 /// <returns>eg: /{yourPath}/2020/11/3/</returns> 8 public static string GetdirPath(string path = "") 9 { 10 DateTime date = DateTime.Now; 11 string timeDir = date.ToString("yyyyMMdd");// date.ToString("yyyyMM/dd/HH/"); 12 13 if (!string.IsNullOrEmpty(path)) 14 { 15 timeDir = Path.Combine(path, timeDir); 16 } 17 return timeDir; 18 } 19 20 /// <summary> 21 /// 取文件名的MD5值(16位) 22 /// </summary> 23 /// <param name="str">文件名,不包括扩展名</param> 24 /// <returns></returns> 25 public static string HashFileName(string str = null) 26 { 27 if (string.IsNullOrEmpty(str)) 28 { 29 str = Guid.NewGuid().ToString(); 30 } 31 MD5 md5 = MD5.Create(); 32 return BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", ""); 33 } 34 35 /// <summary> 36 /// 删除指定目录下的所有文件及文件夹(保留目录) 37 /// </summary> 38 /// <param name="file">文件目录</param> 39 public static void DeleteDirectory(string file) 40 { 41 try 42 { 43 //判断文件夹是否还存在 44 if (Directory.Exists(file)) 45 { 46 DirectoryInfo fileInfo = new DirectoryInfo(file); 47 //去除文件夹的只读属性 48 fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory; 49 foreach (string f in Directory.GetFileSystemEntries(file)) 50 { 51 if (File.Exists(f)) 52 { 53 //去除文件的只读属性 54 File.SetAttributes(file, FileAttributes.Normal); 55 //如果有子文件删除文件 56 File.Delete(f); 57 } 58 else 59 { 60 //循环递归删除子文件夹 61 DeleteDirectory(f); 62 } 63 } 64 //删除空文件夹 65 Directory.Delete(file); 66 } 67 68 } 69 catch (Exception ex) // 异常处理 70 { 71 Console.WriteLine("代码生成异常" + ex.Message); 72 } 73 } 74 75 /// <summary> 76 /// 压缩代码 77 /// </summary> 78 /// <param name="zipPath"></param> 79 /// <param name="genCodePath"></param> 80 /// <param name="zipFileName">压缩后的文件名</param> 81 /// <returns></returns> 82 public static bool ZipGenCode(string zipPath, string genCodePath, string zipFileName) 83 { 84 if (string.IsNullOrEmpty(zipPath)) return false; 85 try 86 { 87 CreateDirectory(genCodePath); 88 string zipFileFullName = Path.Combine(zipPath, zipFileName); 89 if (File.Exists(zipFileFullName)) 90 { 91 File.Delete(zipFileFullName); 92 } 93 94 ZipFile.CreateFromDirectory(genCodePath, zipFileFullName); 95 DeleteDirectory(genCodePath); 96 97 return true; 98 } 99 catch (Exception ex) 100 { 101 Console.WriteLine("压缩文件出错。" + ex.Message); 102 return false; 103 } 104 } 105 106 /// <summary> 107 /// 创建文件夹 108 /// </summary> 109 /// <param name="path"></param> 110 /// <returns></returns> 111 public static bool CreateDirectory(string path) 112 { 113 if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) 114 { 115 path = path.Replace("\\", "/").Replace("//", "/"); 116 } 117 try 118 { 119 if (!Directory.Exists(path)) 120 { 121 DirectoryInfo info = Directory.CreateDirectory(path); 122 Console.WriteLine("不存在创建文件夹" + info); 123 } 124 } 125 catch (Exception ex) 126 { 127 Console.WriteLine($"创建文件夹出错了,{ex.Message}"); 128 return false; 129 } 130 return true; 131 } 132 133 /// <summary> 134 /// 写文件 135 /// </summary> 136 /// <param name="path">完整路径带扩展名的</param> 137 /// <param name="content"></param> 138 public static void WriteAndSave(string path, string content) 139 { 140 if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) 141 { 142 path = path.Replace("\\", "/").Replace("//", "/"); 143 } 144 if (!Directory.Exists(Path.GetDirectoryName(path))) 145 { 146 Directory.CreateDirectory(Path.GetDirectoryName(path)); 147 } 148 Console.WriteLine("开始写入文件,Path=" + path); 149 try 150 { 151 //实例化一个文件流--->与写入文件相关联 152 using var fs = new FileStream(path, FileMode.Create, FileAccess.Write); 153 //实例化一个StreamWriter-->与fs相关联 154 using var sw = new StreamWriter(fs); 155 //开始写入 156 sw.Write(content); 157 //清空缓冲区 158 sw.Flush(); 159 //关闭流 160 sw.Close(); 161 fs.Close(); 162 } 163 catch (Exception ex) 164 { 165 Console.WriteLine("写入文件出错了:" + ex.Message); 166 } 167 } 168 }
上面只是一部分,主要的看重的文件压缩方法,后续会继续增加
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构