DirectoryUtils.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using System.Collections;
  2. using System.IO;
  3. namespace Tede.Data.Tests.Utils
  4. {
  5. /// <summary>
  6. /// 封装操作文件夹代码的类
  7. /// </summary>
  8. public static class DirectoryUtils
  9. {
  10. public static class AspnetClient
  11. {
  12. public const string DirectoryName = "aspnet_client";
  13. }
  14. public static class WwwRoot
  15. {
  16. public const string DirectoryName = "wwwroot";
  17. }
  18. public static class Site
  19. {
  20. public const string Include = "Include";
  21. public const string Template = "Template";
  22. public const string Content = "Content";
  23. }
  24. public static class SiteServer
  25. {
  26. public const string DirectoryName = "SiteServer";
  27. }
  28. public static class Home
  29. {
  30. public const string DirectoryName = "Home";
  31. }
  32. public static class SiteFiles
  33. {
  34. public const string DirectoryName = "SiteFiles";
  35. public const string BackupFiles = "BackupFiles";
  36. public const string TemporaryFiles = "TemporaryFiles";
  37. public const string SiteTemplates = "SiteTemplates";
  38. public const string Plugins = "Plugins";
  39. public const string Packages = "packages";
  40. public const string Home = "Home";
  41. public const string Administrators = "Administrators";
  42. }
  43. public static class SiteTemplates
  44. {
  45. public const string DirectoryName = "SiteTemplates";
  46. //文件夹
  47. public const string SiteTemplateMetadata = "SiteTemplateMetadata"; //存储频道模板元数据的文件夹名称
  48. public const string SiteContent = "SiteContent"; //频道内容导入导出临时文件夹名
  49. public const string Table = "Table"; //辅助表导入导出临时文件夹名
  50. public const string RelatedField = "RelatedField"; //关联字段导入导出临时文件夹名
  51. //文件
  52. public const string FileTemplate = "Template.xml"; //序列化模板的文件名
  53. public const string FileMetadata = "Metadata.xml"; //频道模板元数据文件
  54. public const string FileConfiguration = "Configuration.xml"; //站点配置
  55. }
  56. public static char DirectorySeparatorChar = Path.DirectorySeparatorChar;
  57. public static void CreateDirectoryIfNotExists(string path)
  58. {
  59. var directoryPath = GetDirectoryPath(path);
  60. if (!IsDirectoryExists(directoryPath))
  61. {
  62. try
  63. {
  64. Directory.CreateDirectory(directoryPath);
  65. }
  66. catch
  67. {
  68. //Scripting.FileSystemObject fso = new Scripting.FileSystemObjectClass();
  69. //string[] directoryNames = directoryPath.Split('\\');
  70. //string thePath = directoryNames[0];
  71. //for (int i = 1; i < directoryNames.Length; i++)
  72. //{
  73. // thePath = thePath + "\\" + directoryNames[i];
  74. // if (StringUtils.Contains(thePath.ToLower(), ConfigUtils.Instance.PhysicalApplicationPath.ToLower()) && !IsDirectoryExists(thePath))
  75. // {
  76. // fso.CreateFolder(thePath);
  77. // }
  78. //}
  79. }
  80. }
  81. }
  82. public static void Copy(string sourcePath, string targetPath)
  83. {
  84. Copy(sourcePath, targetPath, true);
  85. }
  86. public static void Copy(string sourcePath, string targetPath, bool isOverride)
  87. {
  88. if (!Directory.Exists(sourcePath)) return;
  89. CreateDirectoryIfNotExists(targetPath);
  90. var directoryInfo = new DirectoryInfo(sourcePath);
  91. foreach (var fileSystemInfo in directoryInfo.GetFileSystemInfos())
  92. {
  93. var destPath = Path.Combine(targetPath, fileSystemInfo.Name);
  94. if (fileSystemInfo is FileInfo)
  95. {
  96. FileUtils.CopyFile(fileSystemInfo.FullName, destPath, isOverride);
  97. }
  98. else if (fileSystemInfo is DirectoryInfo)
  99. {
  100. Copy(fileSystemInfo.FullName, destPath, isOverride);
  101. }
  102. }
  103. }
  104. /// <summary>
  105. /// 验证此字符串是否合作作为文件夹名称
  106. /// </summary>
  107. public static bool IsDirectoryNameCompliant(string directoryName)
  108. {
  109. if (string.IsNullOrEmpty(directoryName)) return false;
  110. return -1 == directoryName.IndexOfAny(PathUtils.InvalidPathChars);
  111. }
  112. /// <summary>
  113. /// 获取文件的文件夹路径,如果path为文件夹,返回自身。
  114. /// </summary>
  115. /// <param name="path">文件路径</param>
  116. /// <returns></returns>
  117. public static string GetDirectoryPath(string path)
  118. {
  119. var ext = Path.GetExtension(path);
  120. var directoryPath = !string.IsNullOrEmpty(ext) ? Path.GetDirectoryName(path) : path;
  121. return directoryPath;
  122. }
  123. public static string GetParentPath(string path, int upLevel = 1)
  124. {
  125. if (upLevel < 1) return path;
  126. for (var i = 0; i < upLevel; i++)
  127. {
  128. path = Directory.GetParent(path).FullName;
  129. }
  130. return path;
  131. }
  132. public static bool IsDirectoryExists(string directoryPath)
  133. {
  134. return Directory.Exists(directoryPath);
  135. }
  136. public static bool IsInDirectory(string parentDirectoryPath, string path)
  137. {
  138. if (string.IsNullOrEmpty(parentDirectoryPath) || string.IsNullOrEmpty(path)) return false;
  139. parentDirectoryPath = parentDirectoryPath.Trim().TrimEnd(Path.DirectorySeparatorChar).ToLower();
  140. path = path.Trim().TrimEnd(Path.DirectorySeparatorChar).ToLower();
  141. return parentDirectoryPath == path || path.StartsWith(parentDirectoryPath);
  142. }
  143. public static void MoveDirectory(string srcDirectoryPath, string destDirectoryPath, bool isOverride)
  144. {
  145. //如果提供的路径中不存在末尾分隔符,则添加末尾分隔符。
  146. if (!srcDirectoryPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
  147. {
  148. srcDirectoryPath += Path.DirectorySeparatorChar;
  149. }
  150. if (!destDirectoryPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
  151. {
  152. destDirectoryPath += Path.DirectorySeparatorChar;
  153. }
  154. //如果目标目录不存在,则予以创建。
  155. CreateDirectoryIfNotExists(destDirectoryPath);
  156. //从当前父目录中获取目录列表。
  157. foreach (var srcDir in Directory.GetDirectories(srcDirectoryPath))
  158. {
  159. var directoryName = PathUtils.GetDirectoryName(srcDir, false);
  160. var destDir = destDirectoryPath + directoryName;
  161. //如果该目录不存在,则创建该目录。
  162. CreateDirectoryIfNotExists(destDir);
  163. //由于我们处于递归模式下,因此还要复制子目录
  164. MoveDirectory(srcDir, destDir, isOverride);
  165. }
  166. //从当前父目录中获取文件。
  167. foreach (var srcFile in Directory.GetFiles(srcDirectoryPath))
  168. {
  169. var srcFileInfo = new FileInfo(srcFile);
  170. var destFileInfo = new FileInfo(srcFile.Replace(srcDirectoryPath, destDirectoryPath));
  171. //如果文件不存在,则进行复制。
  172. var isExists = destFileInfo.Exists;
  173. if (isOverride)
  174. {
  175. if (isExists)
  176. {
  177. FileUtils.DeleteFileIfExists(destFileInfo.FullName);
  178. }
  179. FileUtils.CopyFile(srcFileInfo.FullName, destFileInfo.FullName);
  180. }
  181. else if (!isExists)
  182. {
  183. FileUtils.CopyFile(srcFileInfo.FullName, destFileInfo.FullName);
  184. }
  185. }
  186. }
  187. public static string[] GetDirectoryNames(string directoryPath)
  188. {
  189. var directorys = Directory.GetDirectories(directoryPath);
  190. var retval = new string[directorys.Length];
  191. var i = 0;
  192. foreach (var directory in directorys)
  193. {
  194. var directoryInfo = new DirectoryInfo(directory);
  195. retval[i++] = directoryInfo.Name;
  196. }
  197. return retval;
  198. }
  199. public static ArrayList GetLowerDirectoryNames(string directoryPath)
  200. {
  201. var arraylist = new ArrayList();
  202. var directorys = Directory.GetDirectories(directoryPath);
  203. foreach (var directory in directorys)
  204. {
  205. var directoryInfo = new DirectoryInfo(directory);
  206. arraylist.Add(directoryInfo.Name.ToLower());
  207. }
  208. return arraylist;
  209. }
  210. public static string[] GetFileNames(string directoryPath)
  211. {
  212. var filePaths = Directory.GetFiles(directoryPath);
  213. var retval = new string[filePaths.Length];
  214. var i = 0;
  215. foreach (var filePath in filePaths)
  216. {
  217. var fileInfo = new FileInfo(filePath);
  218. retval[i++] = fileInfo.Name;
  219. }
  220. return retval;
  221. }
  222. /// <summary>
  223. /// 删除文件夹
  224. /// </summary>
  225. /// <param name="directoryPath">文件夹路径</param>
  226. /// <returns>删除过程中是否出错</returns>
  227. public static bool DeleteDirectoryIfExists(string directoryPath)
  228. {
  229. var retval = true;
  230. try
  231. {
  232. if (IsDirectoryExists(directoryPath))
  233. {
  234. Directory.Delete(directoryPath, true);
  235. }
  236. }
  237. catch
  238. {
  239. retval = false;
  240. }
  241. return retval;
  242. }
  243. public static void DeleteFilesSync(string rootDirectoryPath, string syncDirectoryPath)
  244. {
  245. if (IsDirectoryExists(syncDirectoryPath))
  246. {
  247. var directoryInfo = new DirectoryInfo(syncDirectoryPath);
  248. foreach (var fileSystemInfo in directoryInfo.GetFileSystemInfos())
  249. {
  250. var fileSystemPath = PathUtils.Combine(rootDirectoryPath, fileSystemInfo.Name);
  251. if (fileSystemInfo is FileInfo)
  252. {
  253. try
  254. {
  255. FileUtils.DeleteFileIfExists(fileSystemPath);
  256. }
  257. catch
  258. {
  259. // ignored
  260. }
  261. }
  262. else if (fileSystemInfo is DirectoryInfo)
  263. {
  264. DeleteFilesSync(fileSystemPath, fileSystemInfo.FullName);
  265. DeleteEmptyDirectory(fileSystemPath);
  266. }
  267. }
  268. }
  269. }
  270. public static void DeleteEmptyDirectory(string directoryPath)
  271. {
  272. var directoryInfo = new DirectoryInfo(directoryPath);
  273. if (directoryInfo.Exists)
  274. {
  275. if (directoryInfo.GetFileSystemInfos().Length == 0)
  276. {
  277. try
  278. {
  279. DeleteDirectoryIfExists(directoryPath);
  280. }
  281. catch
  282. {
  283. // ignored
  284. }
  285. }
  286. }
  287. }
  288. public static void CreateUrlRedirectDirectory(string sourceUrlRedirectFilePath, string directoryPath)
  289. {
  290. CreateDirectoryIfNotExists(directoryPath);
  291. var filePath = PathUtils.Combine(directoryPath, "default.aspx");
  292. if (!FileUtils.IsFileExists(filePath))
  293. {
  294. FileUtils.CopyFile(sourceUrlRedirectFilePath, filePath);
  295. }
  296. }
  297. public static string[] GetDirectoryPaths(string directoryPath)
  298. {
  299. CreateDirectoryIfNotExists(directoryPath);
  300. return Directory.GetDirectories(directoryPath);
  301. }
  302. public static string[] GetDirectoryPaths(string directoryPath, string searchPattern)
  303. {
  304. CreateDirectoryIfNotExists(directoryPath);
  305. return Directory.GetDirectories(directoryPath, searchPattern);
  306. }
  307. public static string[] GetFilePaths(string directoryPath)
  308. {
  309. return Directory.GetFiles(directoryPath);
  310. }
  311. public static long GetDirectorySize(string directoryPath)
  312. {
  313. long size = 0;
  314. var filePaths = GetFilePaths(directoryPath);
  315. //通过GetFiles方法,获取目录中所有文件的大小
  316. foreach (var filePath in filePaths)
  317. {
  318. var info = new FileInfo(filePath);
  319. size += info.Length;
  320. }
  321. var directoryPaths = GetDirectoryPaths(directoryPath);
  322. //获取目录下所有文件夹大小,并存到一个新的对象数组中
  323. foreach (var path in directoryPaths)
  324. {
  325. size += GetDirectorySize(path);
  326. }
  327. return size;
  328. }
  329. }
  330. }