因为在.net mvc 中 File类命名空间总是冲突,自己封装了一个FileHelper类,今日分享给大家
1 /// <summary> 2 /// Provides helper methods for file operations. 3 /// </summary> 4 public static class FileHelper 5 { 6 7 /// <summary> 8 /// Downloads a file from the specified URL and saves it to the local disk. 9 /// </summary> 10 /// <param name="downLoadUrl">The URL of the file to download.</param> 11 /// <param name="savePath">The path where the file should be saved.</param> 12 /// <param name="fileExtension">The file extension of the downloaded file.</param> 13 /// <param name="fileName">The desired file name (optional). If not provided, a random name will be generated.</param> 14 /// <returns>The full path of the downloaded file.</returns> 15 public static async Task<string> FileDownLoad(string downLoadUrl, string savePath, string fileExtension, string fileName = "") 16 { 17 try 18 { 19 using (HttpClient client = new HttpClient()) 20 { 21 // 发送GET请求并获取数据 22 byte[] fileData = await client.GetByteArrayAsync(downLoadUrl); 23 24 // 保存到本地文件 25 fileName = string.IsNullOrEmpty(fileName) ? CreateFileName(fileExtension) : CreateFileName(fileName, fileExtension); 26 var filepath = Path.Combine(savePath, fileName); 27 File.WriteAllBytes(filepath, fileData); 28 return filepath; 29 } 30 } 31 catch (Exception ex) 32 { 33 throw new Exception($"文件下载错误{ex.Message}"); 34 } 35 } 36 37 /// <summary> 38 /// Creates a directory path based on the input folder path. 39 /// </summary> 40 /// <param name="directoryPath">The folder path.</param> 41 /// <param name="isDelFile">Specifies whether to clear the files in the folder if it already exists.</param> 42 /// <returns>The directory path.</returns> 43 public static string CreateDirectoryPath(string directoryPath, bool isDelFile = false) 44 { 45 if (!Directory.Exists(directoryPath)) 46 { 47 Directory.CreateDirectory(directoryPath); 48 } 49 50 if (isDelFile) 51 { 52 DeleteFolderContents(directoryPath); 53 } 54 Console.WriteLine("CreateDirectoryPath->" + directoryPath); 55 return directoryPath; 56 } 57 58 /// <summary> 59 /// Checks if a file exists. 60 /// </summary> 61 /// <param name="filePath">The file path.</param> 62 /// <returns>True if the file exists, false otherwise.</returns> 63 public static bool FileExists(string filePath) 64 { 65 return File.Exists(filePath); 66 } 67 68 /// <summary> 69 /// Generates a random numeric file name. 70 /// </summary> 71 /// <param name="extension">The file extension.</param> 72 /// <returns>The file name with extension.</returns> 73 public static string CreateFileName(string extension) 74 { 75 return $"{SnowflakeIdGenerator.CreateNextId()}{extension}"; 76 } 77 78 /// <summary> 79 /// Generates a file name with a specified name and extension. 80 /// </summary> 81 /// <param name="fileName">The file name.</param> 82 /// <param name="extension">The file extension.</param> 83 /// <returns>The file name with extension.</returns> 84 public static string CreateFileName(string fileName, string extension) 85 { 86 return $"{fileName}{extension}"; 87 } 88 89 90 /// <summary> 91 /// Converts a file to a byte array. 92 /// </summary> 93 /// <param name="filePath">The file path.</param> 94 /// <returns>The byte array.</returns> 95 public static byte[] FileConvertToByteArray(string filePath) 96 { 97 if (FileExists(filePath)) 98 { 99 return File.ReadAllBytes(filePath); 100 } 101 return null; 102 } 103 104 /// <summary> 105 /// Creates a zip file. 106 /// </summary> 107 /// <param name="sourceFolderPath">The folder path to be compressed.</param> 108 /// <param name="destinationZipPath">The destination path where the zip file will be saved.</param> 109 public static void CreateZipFile(string sourceFolderPath, string destinationZipPath) 110 { 111 FastZip fastZip = new FastZip(); 112 fastZip.CreateZip(destinationZipPath, sourceFolderPath, true, ""); 113 } 114 115 /// <summary> 116 /// Deletes all folders and files in the specified directory. 117 /// </summary> 118 /// <param name="folderPath">The directory path.</param> 119 public static void DeleteFolderContents(string folderPath) 120 { 121 foreach (string file in Directory.GetFiles(folderPath)) 122 { 123 File.Delete(file); 124 } 125 126 foreach (string subfolder in Directory.GetDirectories(folderPath)) 127 { 128 DeleteFolderContents(subfolder); 129 } 130 131 Directory.Delete(folderPath, true); 132 } 133 public static void DeleteFile(string fileFullPath) 134 { 135 if (FileExists(fileFullPath)) 136 { 137 File.Delete(fileFullPath); 138 } 139 } 140 141 /// <summary> 142 /// Saves the given byte array as a file at the specified path. 143 /// </summary> 144 /// <param name="bytes">The byte array to save as a file.</param> 145 /// <param name="saveFileFullPath">The full path to save the file.</param> 146 public static void SaveFile(this byte[] bytes, string saveFileFullPath) 147 { 148 File.WriteAllBytes(saveFileFullPath, bytes); 149 } 150 151 152 153 }
源码地址:https://github.com/yycb1994/DotNet.Working.Notes/tree/main/Working.Notes/Working.Tools