//************************************************************************ // author: yuzhengyang // date: 2017.10.12 - 2017.10.12 // desc: 文件目录工具类 // Copyright (c) yuzhengyang. All rights reserved. //************************************************************************ using Azylee.Core.DataUtils.CollectionUtils; using Azylee.Core.DataUtils.StringUtils; using Azylee.Core.IOUtils.FileUtils; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; namespace Azylee.Core.IOUtils.DirUtils { /// /// 文件目录工具类 /// public class DirTool { /// /// 创建文件目录(文件不存在则创建) /// /// /// /// 如果文件已存在,返回true /// 如果文件不存在,则创建文件,成功返回true,失败返回false /// public static bool Create(string path) { if (Directory.Exists(path)) return true; else try { Directory.CreateDirectory(path); return true; } catch (Exception e) { } return false; } /// /// 获取目录的父目录 /// /// /// public static string Parent(string path) { string p = path; if (!string.IsNullOrWhiteSpace(p)) { while (p.EndsWith("\\")) p = p.Substring(0, p.Length - 1); if (StringTool.SubStringCount(p, "\\") >= 1) { try { return Directory.GetParent(p).ToString(); } catch (Exception e) { } } } return p; } /// /// 获取目录下的目录(一层) /// /// /// public static List GetPath(string path) { if (Directory.Exists(path)) try { return Directory.EnumerateDirectories(path).ToList(); } catch (Exception e) { } return null; } /// /// 获取目录下所有目录(递归) /// /// /// public static List GetAllPath(string path) { List result = GetPath(path); if (!ListTool.IsNullOrEmpty(result)) { List temp = new List(); foreach (var item in result) { List t = GetAllPath(item); if (!ListTool.IsNullOrEmpty(t)) temp.AddRange(t); } result.AddRange(temp); return result; } return null; } /// /// 判断目录是否为磁盘 /// /// /// public static bool IsDriver(string path) { if (path != null && path.Length >= 2) { if (path.Substring(1, 1) == ":") { return true; } } return false; } /// /// 获取文件所在的目录 /// /// /// public static string GetFilePath(string filePath) { string result = ""; if (!string.IsNullOrWhiteSpace(filePath)) { string fileName = Path.GetFileName(filePath); result = filePath.Substring(0, filePath.Length - fileName.Length); } return result; } /// /// 连接多个string构成目录 /// /// /// public static string Combine(params string[] paths) { if (ListTool.HasElements(paths)) { if (paths.Length > 1) { StringBuilder result = new StringBuilder(paths[0]); for (int i = 1; i < paths.Length; i++) { result.Append("\\"); result.Append(paths[i]); } while (result.ToString().IndexOf("\\\\") >= 0) { result.Replace("\\\\", "\\"); } return result.ToString(); } else { return paths[0]; } } return ""; } /// /// 路径包含关系 /// /// /// /// /// -1:不存在包含关系 /// 0:两个目录相同 /// 1:path1 包含 path2(path1 大) /// 2:path2 包含 path1(path2 大) /// public static int Include(string path1, string path2) { if (path1 == path2) return 0;//两个目录相同 string p1 = Combine(path1 + "\\"); string p2 = Combine(path2 + "\\"); if (p1 == p2) return 0;//两个目录相同(防止路径后有带\或不带\的情况) if (p1.Length > p2.Length && p1.Contains(p2)) return 1;//path1 包含 path2(path1 大) if (p2.Length > p1.Length && p2.Contains(p1)) return 2;//path2 包含 path1(path2 大) return -1;//不存在包含关系 } public static string GetPathName(string s) { StringBuilder sb = new StringBuilder(); if (!string.IsNullOrWhiteSpace(s)) { char[] c = s.ToArray(); for (int i = c.Length - 1; i >= 0; i--) { if (c[i] != '\\') { sb.Append(c[i]); } else { if (sb.Length > 0) break; } } char[] mirror = sb.ToString().ToArray(); sb.Clear(); for (int i = mirror.Length - 1; i >= 0; i--) { sb.Append(mirror[i]); } } return sb.ToString(); } public static long GetPathSize(string path) { long size = 0, _s = 0; try { List files = FileTool.GetAllFile(path); if (ListTool.HasElements(files)) { foreach (var f in files) { if (File.Exists(f)) { _s = FileTool.Size(f); if (_s >= 0) size += _s; } } } } catch { } return size; } } }