This commit is contained in:
龙腾猫跃 committed 2026-05-07 07:33:06 +08:00
1 parent c017654b6a
commit 49c2a9c8ff
11 files changed
+161 -154

No files matched your search

+4 -6
View File
@@ -16,16 +16,14 @@ public static class DirectoryUtils {
/// <summary>
/// 判断文件夹是否存在。
/// </summary>
public static bool Exists(string path) {
return Directory.Exists(PathUtils.WithLongPath(path));
}
public static bool Exists(string path)
=> Directory.Exists(PathUtils.WithLongPath(path));
/// <summary>
/// 创建 <see cref="DirectoryInfo"/> 对象。
/// </summary>
public static DirectoryInfo GetInfo(string path) {
return new(PathUtils.WithLongPath(path));
}
public static DirectoryInfo GetInfo(string path)
=> new(PathUtils.WithLongPath(path));
/// <summary>
/// 返回指定路径下的所有文件,不以 \\?\ 开头。
+32 -16
View File
@@ -30,51 +30,67 @@ public static class EscapeUtils {
}
/// <summary>
/// 正则表达式转义。等同于 Regex.Escape。
/// 正则表达式转义。
/// 等同于 <see cref="Regex.Escape"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? RegexEscape(string? value) => Regex.Escape(value);
public static string? RegexEscape(string? value)
=> Regex.Escape(value);
/// <summary>
/// 正则表达式去转义。等同于 Regex.Unescape。
/// 正则表达式去转义。
/// 等同于 <see cref="Regex.Unescape"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? RegexUnescape(string? value) => Regex.Unescape(value);
public static string? RegexUnescape(string? value)
=> Regex.Unescape(value);
/// <summary>
/// URL 转义。等同于 Uri.EscapeDataString。
/// URL 转义。
/// 等同于 <see cref="Uri.EscapeDataString"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? UrlEscape(string? value) => Uri.EscapeDataString(value);
public static string? UrlEscape(string? value)
=> Uri.EscapeDataString(value);
/// <summary>
/// URL 去转义。等同于 Uri.UnescapeDataString。
/// URL 去转义。
/// 等同于 <see cref="Uri.UnescapeDataString"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? UrlUnescape(string? value) => Uri.UnescapeDataString(value);
public static string? UrlUnescape(string? value)
=> Uri.UnescapeDataString(value);
/// <summary>
/// 表单转义。等同于 WebUtility.UrlEncode。
/// 表单转义。
/// 等同于 <see cref="WebUtility.UrlEncode"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? FormUrlEscape(string? value) => WebUtility.UrlEncode(value);
public static string? FormUrlEscape(string? value)
=> WebUtility.UrlEncode(value);
/// <summary>
/// 表单去转义。等同于 WebUtility.UrlDecode。
/// 表单去转义。
/// 等同于 <see cref="WebUtility.UrlDecode"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? FormUrlUnescape(string? value) => WebUtility.UrlDecode(value);
public static string? FormUrlUnescape(string? value)
=> WebUtility.UrlDecode(value);
/// <summary>
/// HTML 转义。等同于 WebUtility.HtmlEncode。
/// HTML 转义。
/// 等同于 <see cref="WebUtility.HtmlEncode"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? HtmlEscape(string? value) => WebUtility.HtmlEncode(value);
public static string? HtmlEscape(string? value)
=> WebUtility.HtmlEncode(value);
/// <summary>
/// HTML 去转义。等同于 WebUtility.HtmlDecode。
/// HTML 去转义。
/// 等同于 <see cref="WebUtility.HtmlDecode"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? HtmlUnescape(string? value) => WebUtility.HtmlDecode(value);
public static string? HtmlUnescape(string? value)
=> WebUtility.HtmlDecode(value);
}
+11 -16
View File
@@ -9,17 +9,15 @@ public static class FileUtils {
/// 写入文件。
/// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。
/// </summary>
public static void Write(string filePath, string text, Encoding? encoding = null) {
Write(filePath, (encoding ?? new UTF8Encoding()).GetBytes(text));
}
public static void Write(string filePath, string text, Encoding? encoding = null)
=> Write(filePath, (encoding ?? new UTF8Encoding()).GetBytes(text));
/// <summary>
/// 写入文件。
/// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。
/// </summary>
public static void Write(string filePath, IEnumerable<byte> content) {
Write(filePath, [..content]);
}
public static void Write(string filePath, IEnumerable<byte> content)
=> Write(filePath, [.. content]);
/// <summary>
/// 写入文件。
@@ -94,7 +92,7 @@ public static class FileUtils {
internalEx?.Throw();
}
}
// 用于删除到回收站的接口
// API
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
static extern int SHCreateItemFromParsingName([MarshalAs(UnmanagedType.LPWStr)] string pszPath, IntPtr pbc, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out IShellItem ppv);
[ComImport, Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
@@ -132,9 +130,8 @@ public static class FileUtils {
/// <summary>
/// 打开该文件的只读 <see cref="FileStream"/>。
/// </summary>
public static FileStream ReadAsStream(string filePath) {
return new(PathUtils.WithLongPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
public static FileStream ReadAsStream(string filePath)
=> new(PathUtils.WithLongPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
/// <summary>
/// 在指定路径创建文件,并打开 <see cref="FileStream"/>。
@@ -260,16 +257,14 @@ public static class FileUtils {
/// <summary>
/// 确定指定的文件是否存在。
/// </summary>
public static bool Exists(string filePath) {
return File.Exists(PathUtils.WithLongPath(filePath));
}
public static bool Exists(string filePath)
=> File.Exists(PathUtils.WithLongPath(filePath));
/// <summary>
/// 创建 <see cref="FileInfo"/> 对象。
/// </summary>
public static FileInfo GetInfo(string path) {
return new(PathUtils.WithLongPath(path));
}
public static FileInfo GetInfo(string path)
=> new(PathUtils.WithLongPath(path));
/// <summary>
/// 复制文件。
-4
View File
@@ -13,7 +13,3 @@ public class HttpRequestCodeException(string message, HttpStatusCode statusCode,
/// </summary>
public string? Response { get; set; } = response;
}
public static class HttpUtils {
}
+9 -5
View File
@@ -30,17 +30,21 @@ public static class MathUtils {
/// 转换为 2 到 86 进制的字符串。
/// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86。
/// </summary>
public static string ConvertRadix(this long input, int toRadix) => ConvertRadix((BigInteger) input, toRadix);
public static string ConvertRadix(this long input, int toRadix)
=> ConvertRadix((BigInteger) input, toRadix);
/// <summary>
/// 转换为 2 到 86 进制的字符串。
/// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86。
/// </summary>
public static string ConvertRadix(this int input, int toRadix) => ConvertRadix((BigInteger) input, toRadix);
public static string ConvertRadix(this int input, int toRadix)
=> ConvertRadix((BigInteger) input, toRadix);
/// <summary>
/// 转换为 2 到 86 进制的字符串。
/// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86。
/// </summary>
public static string ConvertRadix(this byte input, int toRadix) => ConvertRadix((BigInteger) input, toRadix);
public static string ConvertRadix(this byte input, int toRadix)
=> ConvertRadix((BigInteger) input, toRadix);
private static string ConvertRadix(this BigInteger input, int toRadix) {
const string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/+=!?@#$%^&*()[]{}<>;:',";
if (toRadix < 2 || toRadix > digits.Length) throw new ArgumentOutOfRangeException(nameof(toRadix), $"{nameof(toRadix)} must be between 2 and 86.");
@@ -76,8 +80,8 @@ public static class MathUtils {
/// 在一个范围中进行线性插值。
/// 返回值将被舍入到小数点后 7 位,以解决浮点数误差问题。
/// </summary>
public static double Lerp(double start, double end, double percentage) =>
Math.Round(start + (end - start) * percentage, 7);
public static double Lerp(double start, double end, double percentage)
=> Math.Round(start + (end - start) * percentage, 7);
/// <summary>
/// 计算二阶贝塞尔曲线。
+8 -10
View File
@@ -6,16 +6,14 @@ public static class PathUtils {
/// <summary>
/// 确保路径的结尾包含文件夹分隔符。
/// </summary>
public static string WithSeparator(string folder) {
return folder.EndsWithF(Path.DirectorySeparatorChar) ? folder : folder + Path.DirectorySeparatorChar;
}
public static string WithSeparator(string folder)
=> folder.EndsWithF(Path.DirectorySeparatorChar) ? folder : folder + Path.DirectorySeparatorChar;
/// <summary>
/// 确保路径的结尾不包含文件夹分隔符。
/// </summary>
public static string WithoutSeparator(string folder) {
return folder.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
}
public static string WithoutSeparator(string folder)
=> folder.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
#endregion
@@ -59,7 +57,7 @@ public static class PathUtils {
private static extern int GetShortPathNameW(string lpszLongPath, [Out] char[] buffer, int cchBuffer);
/// <summary>
/// 将完整路径转换为以 \\?\ 开头的标准长路径格式。
/// 将完整路径转换为以 <c>\\?\</c> 开头的标准长路径格式。
/// 这会去除路径末尾的分隔符,且将 / 替换为 \。
/// </summary>
public static string WithLongPath(string path) {
@@ -74,10 +72,10 @@ public static class PathUtils {
}
/// <summary>
/// 去除路径开头的 \\?\。
/// 去除路径开头的 <c>\\?\</c>。
/// </summary>
public static string WithoutLongPath(string path) =>
path.AfterLast(@"\\?\");
public static string WithoutLongPath(string path)
=> path.AfterLast(@"\\?\");
#endregion