DirectoryUtils.cs 864 B

12345678910111213141516171819202122232425
  1. namespace MeloongCore;
  2. public static class DirectoryUtils {
  3. /// <summary>
  4. /// 确保路径以 / 或 \ 结尾。
  5. /// </summary>
  6. public static string ToDirectoryFormat(string folder) => IsDirectoryFormat(folder) ? folder : folder + Path.DirectorySeparatorChar;
  7. /// <summary>
  8. /// 检查路径是否以 / 或 \ 结尾。
  9. /// </summary>
  10. public static bool IsDirectoryFormat(string path) => path.EndsWithF("/") || path.EndsWithF("\\");
  11. /// <summary>
  12. /// 创建文件夹,或文件所在的文件夹。
  13. /// 支持长路径。
  14. /// 文件夹已存在时不会抛出异常。
  15. /// </summary>
  16. public static void Create(string path, bool isFilePath = false) {
  17. path = FileUtils.ShortenPath(path);
  18. if (isFilePath) path = Path.GetDirectoryName(path);
  19. Directory.CreateDirectory(path);
  20. }
  21. }