You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
12 KiB

  1. using System.Collections.Generic;
  2. using System.Text;
  3. namespace Apewer.Internals
  4. {
  5. internal class FileHelper
  6. {
  7. /// <summary>获取指定目录下子文件的路径,不递归子目录。</summary>
  8. /// <param name="argDirectory">顶级目录。</param>
  9. public static List<string> GetSubFilePath(string argDirectory)
  10. {
  11. return GetSubFilePath(argDirectory, false, false);
  12. }
  13. /// <summary>获取指定目录下子文件的路径。</summary>
  14. /// <param name="argDirectory">顶级目录。</param>
  15. /// <param name="argRecurSub">递归子目录。</param>
  16. /// <param name="argRecurPrecedence">优先排列递归项。</param>
  17. public static List<string> GetSubFilePath(string argDirectory, bool argRecurSub, bool argRecurPrecedence)
  18. {
  19. return GetSubFilePath(argDirectory, argRecurSub ? -1 : 0, argRecurPrecedence);
  20. }
  21. /// <summary>获取指定目录下子文件的路径。</summary>
  22. /// <param name="argDirectory">顶级目录。</param>
  23. /// <param name="argRecurDepth">子目录递归深度。</param>
  24. /// <param name="argRecurPrecedence">优先排列递归项。</param>
  25. public static List<string> GetSubFilePath(string argDirectory, int argRecurDepth, bool argRecurPrecedence)
  26. {
  27. var list = new List<string>();
  28. if (string.IsNullOrEmpty(argDirectory)) return list;
  29. var directorylist = new List<string>();
  30. if (argRecurDepth == 0)
  31. {
  32. directorylist.Add(argDirectory);
  33. }
  34. else
  35. {
  36. var recurdicrotylist = GetSubDirectoryPath(argDirectory, argRecurDepth, argRecurPrecedence);
  37. if (argRecurPrecedence)
  38. {
  39. directorylist.AddRange(recurdicrotylist);
  40. directorylist.Add(argDirectory);
  41. }
  42. else
  43. {
  44. directorylist.Add(argDirectory);
  45. directorylist.AddRange(recurdicrotylist);
  46. }
  47. }
  48. foreach (var directory in directorylist)
  49. {
  50. try
  51. {
  52. var files = System.IO.Directory.GetFiles(directory);
  53. list.AddRange(files);
  54. }
  55. catch { }
  56. }
  57. return list;
  58. }
  59. /// <summary>获取指定目录下子目录的路径,不递归子目录。</summary>
  60. /// <param name="argDirectory">顶级目录。</param>
  61. public static List<string> GetSubDirectoryPath(string argDirectory)
  62. {
  63. return GetSubDirectoryPath(argDirectory, false, false);
  64. }
  65. /// <summary>获取指定目录下子目录的路径。</summary>
  66. /// <param name="argDirectory">顶级目录。</param>
  67. /// <param name="argRecurSub">递归子目录。</param>
  68. /// <param name="argRecurPrecedence">优先排列递归项。</param>
  69. public static List<string> GetSubDirectoryPath(string argDirectory, bool argRecurSub, bool argRecurPrecedence)
  70. {
  71. return GetSubDirectoryPath(argDirectory, argRecurSub ? -1 : 0, argRecurPrecedence);
  72. }
  73. /// <summary>获取指定目录下子目录的路径。</summary>
  74. /// <param name="argDirectory">顶级目录。</param>
  75. /// <param name="argRecurDepth">子目录递归深度。</param>
  76. /// <param name="argRecurPrecedence">优先排列递归项。</param>
  77. public static List<string> GetSubDirectoryPath(string argDirectory, int argRecurDepth, bool argRecurPrecedence)
  78. {
  79. var list = new List<string>();
  80. if (string.IsNullOrEmpty(argDirectory)) return list;
  81. var directories = new string[0];
  82. try
  83. {
  84. directories = System.IO.Directory.GetDirectories(argDirectory);
  85. }
  86. catch { }
  87. foreach (var directory in directories)
  88. {
  89. var recurlist = new List<string>();
  90. if (argRecurDepth != 0)
  91. {
  92. var depth = (argRecurDepth > 0) ? argRecurDepth - 1 : argRecurDepth;
  93. var subrecur = GetSubDirectoryPath(directory, depth, argRecurPrecedence);
  94. recurlist.AddRange(subrecur);
  95. }
  96. if (argRecurPrecedence)
  97. {
  98. list.AddRange(recurlist);
  99. list.Add(directory);
  100. }
  101. else
  102. {
  103. list.Add(directory);
  104. list.AddRange(recurlist);
  105. }
  106. }
  107. return list;
  108. }
  109. /// <summary>判断文件是否存在。</summary>
  110. /// <param name="argPath">文件路径。</param>
  111. public static bool Exist(string argPath)
  112. {
  113. if (string.IsNullOrEmpty(argPath)) return false;
  114. if (System.IO.File.Exists(argPath)) return true;
  115. if (System.IO.Directory.Exists(argPath)) return true;
  116. return false;
  117. }
  118. /// <summary>判断文件是否存在。</summary>
  119. /// <param name="argPath">文件路径。</param>
  120. public static bool Exists(string argPath)
  121. {
  122. return Exist(argPath);
  123. }
  124. /// <summary>获取文件流,并锁定文件。若文件不存在,则先创建文件;若获取失败,则返回 NULL 值。</summary>
  125. /// <param name="argPath">文件路径。</param>
  126. public static System.IO.Stream Open(string argPath)
  127. {
  128. return Open(argPath, false);
  129. }
  130. /// <summary>获取文件流。若文件不存在,则先创建文件;若获取失败,则返回 NULL 值。</summary>
  131. /// <param name="argPath">文件路径。</param>
  132. /// <param name="argShare">共享。</param>
  133. public static System.IO.Stream Open(string argPath, bool argShare)
  134. {
  135. try
  136. {
  137. if (!string.IsNullOrEmpty(argPath))
  138. {
  139. var mode = System.IO.FileMode.OpenOrCreate;
  140. var access = System.IO.FileAccess.ReadWrite;
  141. var share = argShare ? System.IO.FileShare.ReadWrite : System.IO.FileShare.None;
  142. var stream = new System.IO.FileStream(argPath, mode, access, share);
  143. return stream;
  144. }
  145. }
  146. catch { }
  147. return null;
  148. }
  149. /// <summary>创建一个空文件且不保留句柄。</summary>
  150. /// <param name="argPath">文件路径,若已存在则返回失败。</param>
  151. /// <param name="argReplace">替换现有文件。</param>
  152. /// <returns>创建成功。</returns>
  153. public static bool Create(string argPath, bool argReplace = false)
  154. {
  155. try
  156. {
  157. if (string.IsNullOrEmpty(argPath)) return false;
  158. if (argReplace) { if (!Delete(argPath)) return false; }
  159. else { if (Exist(argPath)) return false; }
  160. if (!Exist(argPath))
  161. {
  162. var pd = System.IO.Directory.GetParent(argPath).FullName;
  163. System.IO.Directory.CreateDirectory(pd);
  164. var bs = Constant.EmptyBytes;
  165. Write(argPath, bs);
  166. return true;
  167. }
  168. }
  169. catch { }
  170. return false;
  171. }
  172. /// <summary>向文件写入数据。文件不存在将创建,存在则覆盖。</summary>
  173. public static bool Write(string argPath, byte[] argData)
  174. {
  175. try { System.IO.File.WriteAllBytes(argPath, argData); return true; }
  176. catch { return false; }
  177. }
  178. /// <summary>向文件写入文本。文件不存在将创建,存在则覆盖。</summary>
  179. public static bool Write(string argPath, string argText)
  180. {
  181. try { System.IO.File.WriteAllText(argPath, argText); return true; }
  182. catch { return false; }
  183. }
  184. /// <summary>向文件写入文本。文件不存在将创建,存在则覆盖。</summary>
  185. public static bool Write(string argPath, string argText, Encoding argEncoding)
  186. {
  187. try { System.IO.File.WriteAllText(argPath, argText, argEncoding); return true; }
  188. catch { return false; }
  189. }
  190. /// <summary>向文件追加数据。文件不存在将创建,存在则覆盖。</summary>
  191. public static bool Append(string argPath, params byte[] argData)
  192. {
  193. var result = false;
  194. System.IO.FileStream file = null;
  195. try
  196. {
  197. file = new System.IO.FileStream(argPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
  198. file.Position = file.Length;
  199. //vfile.Seek(vfile.Length, SeekOrigin.End);
  200. file.Write(argData, 0, argData.Length);
  201. file.Flush();
  202. file.Close();
  203. result = true;
  204. }
  205. finally
  206. {
  207. if (file != null)
  208. {
  209. file.Dispose();
  210. file = null;
  211. }
  212. }
  213. return result;
  214. }
  215. /// <summary>读取文件中的数据。</summary>
  216. public static byte[] Read(string argPath)
  217. {
  218. try { if (Exist(argPath)) return System.IO.File.ReadAllBytes(argPath); } catch { }
  219. return Constant.EmptyBytes;
  220. }
  221. /// <summary>读取文件中的数据,可选择去除 BOM 字节。</summary>
  222. public static byte[] Read(string argPath, bool argWipeBom)
  223. {
  224. try
  225. {
  226. if (Exist(argPath))
  227. {
  228. var bytes = System.IO.File.ReadAllBytes(argPath);
  229. if (argWipeBom) bytes = ByteHelper.WipeTextBom(bytes);
  230. return bytes;
  231. }
  232. }
  233. catch { }
  234. return Constant.EmptyBytes;
  235. }
  236. /// <summary>读取文件中的文本。</summary>
  237. public static string Read(string argPath, Encoding argEncoding)
  238. {
  239. try { if (Exist(argPath)) return System.IO.File.ReadAllText(argPath, argEncoding); } catch { }
  240. return Constant.EmptyString;
  241. }
  242. /// <summary>删除文件。</summary>
  243. /// <param name="argPath">文件路径,若不存在文件则返回成功。</param>
  244. /// <returns>删除成功。</returns>
  245. public static bool Delete(string argPath)
  246. {
  247. if (string.IsNullOrEmpty(argPath)) return false;
  248. if (!Exist(argPath)) return true;
  249. try
  250. {
  251. if (Exist(argPath)) System.IO.File.Delete(argPath);
  252. if (System.IO.Directory.Exists(argPath)) System.IO.Directory.Delete(argPath);
  253. return !Exist(argPath);
  254. }
  255. catch { return false; }
  256. }
  257. /// <summary>复制文件,如果新路径已存在则不复制且返回 False 值。</summary>
  258. public static bool Copy(string argOldPath, string argNewPath)
  259. {
  260. return Copy(argOldPath, argNewPath, false);
  261. }
  262. /// <summary>复制文件。</summary>
  263. /// <param name="argOldPath">旧路径。</param>
  264. /// <param name="argNewPath">新路径。</param>
  265. /// <param name="argReplace">新路劲存在时,替换新文件。</param>
  266. public static bool Copy(string argOldPath, string argNewPath, bool argReplace)
  267. {
  268. if (string.IsNullOrEmpty(argOldPath)) return false;
  269. if (string.IsNullOrEmpty(argNewPath)) return false;
  270. if (!Exist(argOldPath)) return false;
  271. try
  272. {
  273. System.IO.File.Copy(argOldPath, argNewPath, argReplace);
  274. return true;
  275. }
  276. catch { return false; }
  277. }
  278. /// <summary>确保目录存在,若不存在则创建,返回目录的存在状态。</summary>
  279. public static bool AssureDirectory(string argPath)
  280. {
  281. if (string.IsNullOrEmpty(argPath)) return false;
  282. try
  283. {
  284. if (System.IO.Directory.Exists(argPath)) return true;
  285. var cd = System.IO.Directory.CreateDirectory(argPath);
  286. return cd.Exists;
  287. }
  288. catch { return false; }
  289. }
  290. }
  291. }