ref: PathUtils.RemoveFileName

This commit is contained in:
龙腾猫跃 committed 2026-05-07 10:23:17 +08:00
1 parent 6bce8a6515
commit b26e2c77e6
3 files changed
+29 -15

No files matched your search

+12 -12
View File
@@ -94,38 +94,38 @@ public static class StringExtensions {
} }
/// <summary> /// <summary>
/// 获取在子字符串第一次出现之前的部分,例如对 2024/11/08 拆切 / 会得到 2024。 /// 获取在子字符串第一次出现之前的部分,如果未找到子字符串则不裁切。
/// 如果未找到子字符串则不裁切。 /// <code>"2024/11/08".BeforeFirst("/") => "2024"</code>
/// </summary> /// </summary>
public static string BeforeFirst(this string str, [AllowNull] string text, bool ignoreCase = false) { public static string BeforeFirst(this string str, string text, bool ignoreCase = false) {
int pos = string.IsNullOrEmpty(text) ? -1 : str.IndexOfF(text!, ignoreCase); int pos = string.IsNullOrEmpty(text) ? -1 : str.IndexOfF(text!, ignoreCase);
if (pos < 0) return str; if (pos < 0) return str;
return str.Substring(0, pos); return str.Substring(0, pos);
} }
/// <summary> /// <summary>
/// 获取在子字符串最后一次出现之前的部分,例如对 2024/11/08 拆切 / 会得到 2024/11。 /// 获取在子字符串最后一次出现之前的部分,如果未找到子字符串则不裁切。
/// 如果未找到子字符串则不裁切。 /// <code>"2024/11/08".BeforeLast("/") => "2024/11"</code>
/// </summary> /// </summary>
public static string BeforeLast(this string str, [AllowNull] string text, bool ignoreCase = false) { public static string BeforeLast(this string str, string text, bool ignoreCase = false) {
int pos = string.IsNullOrEmpty(text) ? -1 : str.LastIndexOfF(text!, ignoreCase); int pos = string.IsNullOrEmpty(text) ? -1 : str.LastIndexOfF(text!, ignoreCase);
if (pos < 0) return str; if (pos < 0) return str;
return str.Substring(0, pos); return str.Substring(0, pos);
} }
/// <summary> /// <summary>
/// 获取在子字符串第一次出现之后的部分,例如对 2024/11/08 拆切 / 会得到 11/08。 /// 获取在子字符串第一次出现之后的部分,如果未找到子字符串则不裁切。
/// 如果未找到子字符串则不裁切。 /// <code>"2024/11/08".AfterFirst("/") => "11/08"</code>
/// </summary> /// </summary>
public static string AfterFirst(this string str, [AllowNull] string text, bool ignoreCase = false) { public static string AfterFirst(this string str, string text, bool ignoreCase = false) {
int pos = string.IsNullOrEmpty(text) ? -1 : str.IndexOfF(text!, ignoreCase); int pos = string.IsNullOrEmpty(text) ? -1 : str.IndexOfF(text!, ignoreCase);
if (pos < 0) return str; if (pos < 0) return str;
return str.Substring(pos + text!.Length); return str.Substring(pos + text!.Length);
} }
/// <summary> /// <summary>
/// 获取在子字符串最后一次出现之后的部分,例如对 2024/11/08 拆切 / 会得到 08。 /// 获取在子字符串最后一次出现之后的部分,如果未找到子字符串则不裁切。
/// 如果未找到子字符串则不裁切。 /// <code>"2024/11/08".AfterLast("/") => "08"</code>
/// </summary> /// </summary>
public static string AfterLast(this string str, [AllowNull] string text, bool ignoreCase = false) { public static string AfterLast(this string str, string text, bool ignoreCase = false) {
int pos = string.IsNullOrEmpty(text) ? -1 : str.LastIndexOfF(text!, ignoreCase); int pos = string.IsNullOrEmpty(text) ? -1 : str.LastIndexOfF(text!, ignoreCase);
if (pos < 0) return str; if (pos < 0) return str;
return str.Substring(pos + text!.Length); return str.Substring(pos + text!.Length);
+1 -1
View File
@@ -6,7 +6,7 @@ public static class DirectoryUtils {
/// 文件夹已存在时不会抛出异常。 /// 文件夹已存在时不会抛出异常。
/// </summary> /// </summary>
public static void Create(string path, bool isFilePath = false) { public static void Create(string path, bool isFilePath = false) {
if (isFilePath) path = Path.GetDirectoryName(path); if (isFilePath) path = PathUtils.RemoveFileName(path);
if (Exists(path)) return; if (Exists(path)) return;
Logger.Trace($"新建文件夹:{path}"); Logger.Trace($"新建文件夹:{path}");
path = PathUtils.WithLongPath(path); path = PathUtils.WithLongPath(path);
+16 -2
View File
@@ -7,7 +7,7 @@ public static class PathUtils {
/// 确保路径的结尾包含文件夹分隔符。 /// 确保路径的结尾包含文件夹分隔符。
/// </summary> /// </summary>
public static string WithSeparator(string folder) public static string WithSeparator(string folder)
=> folder.EndsWithF(Path.DirectorySeparatorChar) ? folder : folder + Path.DirectorySeparatorChar; => folder.EndsWithF(Path.DirectorySeparatorChar) || folder.EndsWithF(Path.AltDirectorySeparatorChar) ? folder : folder + Path.DirectorySeparatorChar;
/// <summary> /// <summary>
/// 确保路径的结尾不包含文件夹分隔符。 /// 确保路径的结尾不包含文件夹分隔符。
@@ -34,7 +34,7 @@ public static class PathUtils {
if (fullName.EndsWithF(".jar", true)) keepFileName = true; // jar 文件的文件名需要保留原样,否则会导致 Forge 1.20.1 无法通过文件名识别模块名 if (fullName.EndsWithF(".jar", true)) keepFileName = true; // jar 文件的文件名需要保留原样,否则会导致 Forge 1.20.1 无法通过文件名识别模块名
if (keepFileName && FileUtils.Exists(fullName)) { if (keepFileName && FileUtils.Exists(fullName)) {
pathToKeep = Path.GetFileName(fullName); pathToKeep = Path.GetFileName(fullName);
pathToShorten = Path.GetDirectoryName(fullName); pathToShorten = PathUtils.RemoveFileName(fullName);
} }
// 逐级向上寻找已存在的文件夹,将不存在的部分挪到 suffix,不再缩短 // 逐级向上寻找已存在的文件夹,将不存在的部分挪到 suffix,不再缩短
@@ -82,4 +82,18 @@ public static class PathUtils {
#endregion #endregion
#region 路径处理
/// <summary>
/// 去除路径末尾的文件名。
/// 若路径以分隔符结尾,则不作处理。
/// </summary>
public static string? RemoveFileName(string? path) {
if (path is null || string.IsNullOrEmpty(path)) return path;
if (path.EndsWithF(Path.DirectorySeparatorChar) || path.EndsWithF(Path.AltDirectorySeparatorChar)) return path;
return Path.GetDirectoryName(path) ?? "";
}
#endregion
} }