Paths.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace WWPipeLine.Commons
  7. {
  8. public class Paths
  9. {
  10. private static string ApplicationExePath
  11. {
  12. get
  13. {
  14. string codeBase = Assembly.GetExecutingAssembly().Location;
  15. UriBuilder uri = new UriBuilder(codeBase);
  16. string path = Uri.UnescapeDataString(uri.Path);
  17. return Path.GetDirectoryName(path);
  18. }
  19. }
  20. public static string ApplicationPath
  21. {
  22. get
  23. {
  24. FileInfo f = new FileInfo(ApplicationExePath);
  25. return f.Directory.FullName;
  26. }
  27. }
  28. public static string ApplicationConfigDir
  29. {
  30. get
  31. {
  32. FileInfo f = new FileInfo(ApplicationPath);
  33. return f.FullName + "\\configs";
  34. //return f.FullName + "/configs";
  35. }
  36. }
  37. public static string ApplicationPictureDir
  38. {
  39. get
  40. {
  41. FileInfo f = new FileInfo(ApplicationPath);
  42. return f.FullName + "\\pictures";
  43. //return f.FullName + "/configs";
  44. }
  45. }
  46. public static string ApplicationResourcesDir
  47. {
  48. get
  49. {
  50. FileInfo f = new FileInfo(ApplicationPath);
  51. return f.FullName + "\\resources";
  52. }
  53. }
  54. public static string ApplicationLogDir
  55. {
  56. get
  57. {
  58. FileInfo f = new FileInfo(ApplicationPath);
  59. return f.FullName + "\\logs";
  60. }
  61. }
  62. /// <summary>
  63. /// 临时文件路径
  64. /// </summary>
  65. public static string ApplicationTempResources
  66. {
  67. get
  68. {
  69. FileInfo f = new FileInfo(ApplicationPath);
  70. string path = f.FullName + "\\temp";
  71. if (!System.IO.Directory.Exists(path))
  72. {
  73. Directory.CreateDirectory(path);
  74. }
  75. return path;
  76. }
  77. }
  78. /// <summary>
  79. /// APPLow
  80. /// </summary>
  81. public static string AppLocalLowPath
  82. {
  83. get
  84. {
  85. return Path.Combine(Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)), "LocalLow");
  86. }
  87. }
  88. /// <summary>
  89. /// 系统帮助文档路径
  90. /// </summary>
  91. public static string ApplicationHelpDocument
  92. {
  93. get
  94. {
  95. FileInfo f = new FileInfo(ApplicationPath);
  96. return f.FullName + "\\document\\Help.pdf";
  97. }
  98. }
  99. internal class FileTimeInfo
  100. {
  101. public string FileName; //文件名
  102. public DateTime FileCreateTime; //创建时间
  103. public FileInfo FileInfo;
  104. }
  105. /// <summary>
  106. /// 获取目录下指定后缀的文件
  107. /// </summary>
  108. /// <param name="dir"></param>
  109. /// <param name="ext"></param>
  110. /// <returns></returns>
  111. public static List<FileInfo> FilesInDir(string dir, string ext)
  112. {
  113. List<FileInfo> list = new List<FileInfo>();
  114. DirectoryInfo d = new DirectoryInfo(dir);
  115. foreach (FileInfo fi in d.GetFiles())
  116. {
  117. if (string.IsNullOrEmpty(ext) || fi.Extension.ToUpper() == ext.ToUpper())
  118. {
  119. list.Add(fi);
  120. }
  121. }
  122. return list;
  123. }
  124. /// <summary>
  125. /// 获取某个路径下的最新文件
  126. /// </summary>
  127. /// <param name="dir"></param>
  128. /// <param name="ext"></param>
  129. /// <returns></returns>
  130. public static FileInfo FileInDirLastCreated(string dir, string ext, string extAnother = null)
  131. {
  132. if (!Directory.Exists(dir))
  133. {
  134. throw new FileNotFoundException(dir + " 未找到");
  135. }
  136. FileTimeInfo fi = GetLatestFileTimeInfo(dir, ext, extAnother);
  137. return new FileInfo(fi.FileName);
  138. }
  139. //获取最近创建的文件名和创建时间
  140. //如果没有指定类型的文件,返回null
  141. static FileTimeInfo GetLatestFileTimeInfo(string dir, string ext, string extAnother = null)
  142. {
  143. List<FileTimeInfo> list = new List<FileTimeInfo>();
  144. DirectoryInfo d = new DirectoryInfo(dir);
  145. foreach (FileInfo fi in d.GetFiles())
  146. {
  147. if (string.IsNullOrEmpty(ext) || fi.Extension.ToUpper() == ext.ToUpper())
  148. {
  149. list.Add(new FileTimeInfo()
  150. {
  151. FileName = fi.FullName,
  152. FileCreateTime = fi.CreationTime,
  153. FileInfo = fi
  154. });
  155. }
  156. }
  157. if (list.Count == 0)
  158. {
  159. foreach (FileInfo fi in d.GetFiles())
  160. {
  161. if (string.IsNullOrEmpty(extAnother) || fi.Extension.ToUpper() == extAnother.ToUpper())
  162. {
  163. list.Add(new FileTimeInfo()
  164. {
  165. FileName = fi.FullName,
  166. FileCreateTime = fi.CreationTime,
  167. FileInfo = fi
  168. });
  169. }
  170. }
  171. }
  172. var qry = from x in list
  173. orderby x.FileCreateTime
  174. select x;
  175. return qry.LastOrDefault();
  176. }
  177. public static void ClearFolder(string dir)
  178. {
  179. foreach (string d in Directory.GetFileSystemEntries(dir))
  180. {
  181. if (System.IO.File.Exists(d))
  182. {
  183. FileInfo fi = new FileInfo(d);
  184. if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
  185. fi.Attributes = FileAttributes.Normal;
  186. System.IO.File.Delete(d);//直接删除其中的文件
  187. }
  188. else
  189. {
  190. DirectoryInfo d1 = new DirectoryInfo(d);
  191. if (d1.GetFiles().Length != 0)
  192. {
  193. ClearFolder(d1.FullName);////递归删除子文件夹
  194. }
  195. Directory.Delete(d);
  196. }
  197. }
  198. }
  199. }
  200. }