File.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. namespace XGame.Framework.FileSystem
  2. {
  3. /// <summary>
  4. /// 文件操作
  5. /// </summary>
  6. public static partial class File
  7. {
  8. private static readonly IFileOperator _fileOperator = CreateFileOperator();
  9. private static readonly IDirectoryOperator _directoryOperator = CreateDirectoryOperator();
  10. #region File Operator
  11. /// <summary>
  12. /// 获取文件名
  13. /// </summary>
  14. /// <param name="fullName"></param>
  15. /// <param name="withoutExt"></param>
  16. /// <returns></returns>
  17. public static string GetFileName(string fullName, bool withoutExt = true)
  18. {
  19. return _fileOperator.GetFileName(fullName, withoutExt);
  20. }
  21. /// <summary>
  22. /// 拓展名
  23. /// </summary>
  24. /// <param name="fullName">文件全路径(含拓展名)</param>
  25. /// <returns>拓展名(不包含“.”)</returns>
  26. public static string Extension(string fullName)
  27. {
  28. return _fileOperator.Extension(fullName);
  29. }
  30. /// <summary>
  31. /// 文件是否存在(包外全路径)
  32. /// </summary>
  33. /// <param name="fullName">文件全路径(含拓展名)</param>
  34. /// <returns>True: 存在; False: 不存在; </returns>
  35. public static bool ExistFile(string fullName)
  36. {
  37. return _fileOperator.Exist(fullName);
  38. }
  39. /// <summary>
  40. /// 删除文件(包外全路径)
  41. /// </summary>
  42. /// <param name="fullName">文件全路径(含拓展名)</param>
  43. /// <returns>True: 删除成功; False: 删除失败;</returns>
  44. public static bool DeleteFile(string fullName)
  45. {
  46. return _fileOperator.Delete(fullName);
  47. }
  48. /// <summary>
  49. /// 获取文件所在文件夹路径(包外全路径)
  50. /// </summary>
  51. /// <param name="fullName">文件全路径(含拓展名)</param>
  52. /// <returns>文件夹全路径</returns>
  53. public static string GetDirectory(string fullName)
  54. {
  55. return _fileOperator.GetDirectory(fullName);
  56. }
  57. /// <summary>
  58. /// 复制文件(包外全路径)
  59. /// </summary>
  60. /// <param name="source">源文件</param>
  61. /// <param name="destination">目标文件路径</param>
  62. /// <returns>True: 复制成功; False: 复制失败;</returns>
  63. public static bool CopyFile(string source, string destination)
  64. {
  65. return _fileOperator.Copy(source, destination, true);
  66. }
  67. /// <summary>
  68. /// 异步复制文件
  69. /// </summary>
  70. /// <param name="source">源文件</param>
  71. /// <param name="destination">包外文件全路径</param>
  72. /// <param name="overwrite">覆盖</param>
  73. /// <returns>复制文件异步对象</returns>
  74. public static ICopyFileAsync CopyFileAsync(string source, string destination, bool overwrite = true)
  75. {
  76. return _fileOperator.CopyFileAsync(source, destination, overwrite);
  77. }
  78. /// <summary>
  79. /// 移动文件(包外全路径)
  80. /// </summary>
  81. /// <param name="source">源文件</param>
  82. /// <param name="destination">目标文件路径</param>
  83. public static void MoveFile(string source, string destination)
  84. {
  85. _fileOperator.Move(source, destination);
  86. }
  87. /// <summary>
  88. /// 写入字节数组(包外全路径)
  89. /// </summary>
  90. /// <param name="filePath">文件全路径(包外)</param>
  91. /// <param name="bytes">内容</param>
  92. public static void WriteAllBytes(string filePath, byte[] bytes)
  93. {
  94. _fileOperator.WriteAllBytes(filePath, bytes);
  95. }
  96. /// <summary>
  97. /// 写入文本(包外全路径)
  98. /// </summary>
  99. /// <param name="filePath">文件全路径(包外)</param>
  100. /// <param name="contents"></param>
  101. public static void WriteAllText(string filePath, string contents)
  102. {
  103. _fileOperator.WriteAllText(filePath, contents);
  104. }
  105. /// <summary>
  106. /// 读取文件
  107. /// </summary>
  108. /// <param name="path">文件全路径</param>
  109. /// <returns>字节流</returns>
  110. public static byte[] ReadAllBytes(string path)
  111. {
  112. return _fileOperator.ReadAllBytes(path);
  113. }
  114. /// <summary>
  115. /// 读取文本(UTF-8)
  116. /// </summary>
  117. /// <param name="path">文件全路径</param>
  118. /// <returns>文本</returns>
  119. public static string ReadAllText(string path)
  120. {
  121. return _fileOperator.ReadAllText(path);
  122. }
  123. /// <summary>
  124. /// 异步读取文件
  125. /// </summary>
  126. /// <param name="filePath">文件全路径(包内包外都可以)</param>
  127. /// <returns>文件内容</returns>
  128. public static IReadFileAsync ReadFileAsync(string filePath)
  129. {
  130. return _fileOperator.ReadFileAsync(filePath);
  131. }
  132. /// <summary>
  133. /// 重命名文件
  134. /// </summary>
  135. /// <param name="from"></param>
  136. /// <param name="to"></param>
  137. /// <returns></returns>
  138. public static bool RenameFile(string from, string to)
  139. {
  140. return _fileOperator.Rename(from, to);
  141. }
  142. #endregion
  143. #region Directory Operator
  144. /// <summary>
  145. /// 新建文件夹(逐级新建)
  146. /// </summary>
  147. /// <param name="fullDirectory">文件夹全路径</param>
  148. /// <returns>True: 新建成功; False: 新建失败</returns>
  149. public static bool Mkdir(string fullDirectory)
  150. {
  151. return _directoryOperator.Mkdir(fullDirectory);
  152. }
  153. /// <summary>
  154. /// 删除文件夹(包外全路径)
  155. /// </summary>
  156. /// <param name="fullDirectory">文件夹全路径</param>
  157. /// <param name="includeSelf">是否删除自身文件夹</param>
  158. /// <returns>True: 删除成功; False: 删除失败;</returns>
  159. public static bool DeleteDirectory(string fullDirectory, bool includeSelf = true)
  160. {
  161. return _directoryOperator.Delete(fullDirectory, includeSelf);
  162. }
  163. /// <summary>
  164. /// 文件夹是否存在(包外全路径)
  165. /// </summary>
  166. /// <param name="fullDirectory">文件夹全路径</param>
  167. /// <returns>True: 存在; False: 不存在;</returns>
  168. public static bool ExistDirectory(string fullDirectory)
  169. {
  170. return _directoryOperator.Exist(fullDirectory);
  171. }
  172. /// <summary>
  173. /// 是否是文件夹(包外全路径)
  174. /// </summary>
  175. /// <param name="fullDirectory">文件夹全路径</param>
  176. /// <returns>True: 是; False: 不是;</returns>
  177. public static bool IsDirectory(string fullDirectory)
  178. {
  179. return _directoryOperator.Exist(fullDirectory);
  180. }
  181. /// <summary>
  182. /// 复制(含子文件夹和子文件)
  183. /// </summary>
  184. /// <param name="source">源文件夹</param>
  185. /// <param name="destination">目标文件夹(清空)</param>
  186. /// <returns>True: 复制成功; False: 复制失败</returns>
  187. public static bool CopyDirectory(string source, string destination)
  188. {
  189. return _directoryOperator.Copy(source, destination, true);
  190. }
  191. /// <summary>
  192. /// 获取文件夹名
  193. /// </summary>
  194. /// <param name="fullDirectory">文件夹全路径</param>
  195. /// <returns>文件夹名(最后一个"/")</returns>
  196. public static string GetDirectoryName(string fullDirectory)
  197. {
  198. return _directoryOperator.GetDirectoryName(fullDirectory);
  199. }
  200. /// <summary>
  201. /// 获取文件夹下的所有文件和文件夹路径
  202. /// </summary>
  203. /// <param name="fullDirectory">文件夹全路径</param>
  204. /// <returns></returns>
  205. public static string[] GetFileSystemEntries(string fullDirectory)
  206. {
  207. return _directoryOperator.GetFileSystemEntries(fullDirectory);
  208. }
  209. /// <summary>
  210. /// 异步删除文件夹
  211. /// </summary>
  212. /// <param name="fullDirectory">文件夹全路径</param>
  213. /// <param name="includeSelf">是否删除自身文件夹</param>
  214. /// <returns>True: 删除成功; False: 删除失败;</returns>
  215. public static IDeleteDirectoryAsync DeleteAsync(string fullDirectory, bool includeSelf = true)
  216. {
  217. return _directoryOperator.DeleteAsync(fullDirectory, includeSelf);
  218. }
  219. /// <summary>
  220. /// 重命名文件夹
  221. /// </summary>
  222. /// <param name="from"></param>
  223. /// <param name="to"></param>
  224. /// <returns></returns>
  225. public static bool RenameDirectory(string from, string to)
  226. {
  227. Assert.IsTrue(ExistDirectory(from), $"File.RenameDirectory: {from} 文件夹不存在!!");
  228. Assert.IsTrue(!ExistDirectory(to), $"File.RenameDirectory: {to} 文件夹已存在!!");
  229. return _directoryOperator.Rename(from, to);
  230. }
  231. /// <summary>
  232. /// 获取文件夹下所有文件
  233. /// </summary>
  234. /// <param name="fullDirectory">根文件夹全路径</param>
  235. /// <param name="recursion">递归</param>
  236. /// <returns>文件全路径</returns>
  237. public static string[] GetFiles(string fullDirectory, bool recursion = true)
  238. {
  239. return _directoryOperator.GetFiles(fullDirectory, null, recursion);
  240. }
  241. /// <summary>
  242. /// 获取文件夹下指定拓展名文件
  243. /// </summary>
  244. /// <param name="fullDirectory">根文件夹全路径</param>
  245. /// <param name="extension">拓展名(".xxx")</param>
  246. /// <param name="recursion">递归</param>
  247. /// <returns>文件全路径</returns>
  248. public static string[] GetFiles(string fullDirectory, string extension, bool recursion = true)
  249. {
  250. return _directoryOperator.GetFiles(fullDirectory, extension, recursion);
  251. }
  252. #endregion
  253. #region Factory
  254. static IFileOperator CreateFileOperator()
  255. {
  256. return new CSharpFileOperator();
  257. }
  258. static IDirectoryOperator CreateDirectoryOperator()
  259. {
  260. return new CSharpDirectoryOperator();
  261. }
  262. #endregion
  263. #region Path
  264. /// <summary>
  265. /// 路径拼接
  266. /// </summary>
  267. /// <param name="paths"></param>
  268. /// <returns></returns>
  269. public static string Combine(params string[] paths)
  270. {
  271. return System.IO.Path.Combine(paths).Replace('\\', '/');
  272. }
  273. #endregion
  274. }
  275. }