2026-03-01 17:03:49 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
namespace CMLeonOS.Utils
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class PathUtil
|
|
|
|
|
|
{
|
|
|
|
|
|
public static string Sanitize(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
return path;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string sanitized = path;
|
|
|
|
|
|
|
2026-03-01 19:17:10 +08:00
|
|
|
|
// 保留驱动器字母和冒号(如 0:)
|
|
|
|
|
|
// 检查格式:X:\ 其中 X 是单个字符(字母或数字)
|
|
|
|
|
|
bool hasDrive = sanitized.Length >= 2 && sanitized[1] == ':';
|
|
|
|
|
|
string drive = hasDrive ? sanitized.Substring(0, 2) : "";
|
|
|
|
|
|
string rest = hasDrive ? sanitized.Substring(2) : sanitized;
|
2026-03-01 17:03:49 +08:00
|
|
|
|
|
2026-03-01 19:17:10 +08:00
|
|
|
|
// 不替换反斜杠,因为 Cosmos 文件系统需要反斜杠
|
|
|
|
|
|
rest = rest.Replace(':', '_');
|
|
|
|
|
|
rest = rest.Replace('*', '_');
|
|
|
|
|
|
rest = rest.Replace('?', '_');
|
|
|
|
|
|
rest = rest.Replace('"', '_');
|
|
|
|
|
|
rest = rest.Replace('<', '_');
|
|
|
|
|
|
rest = rest.Replace('>', '_');
|
|
|
|
|
|
rest = rest.Replace('|', '_');
|
2026-03-01 17:03:49 +08:00
|
|
|
|
|
2026-03-01 19:17:10 +08:00
|
|
|
|
// 只移除结尾的斜杠,保留开头的斜杠(因为驱动器后面需要斜杠)
|
|
|
|
|
|
rest = rest.TrimEnd('/');
|
|
|
|
|
|
rest = rest.TrimEnd('\\');
|
|
|
|
|
|
|
|
|
|
|
|
return drive + rest;
|
2026-03-01 17:03:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string Combine(string path1, string path2)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Path.Combine(path1, path2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetExtension(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Path.GetExtension(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetFileName(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Path.GetFileName(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetDirectoryName(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Path.GetDirectoryName(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool Exists(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return File.Exists(path) || Directory.Exists(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string Normalize(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
return path;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string normalized = path.Replace('\\', '/');
|
|
|
|
|
|
|
|
|
|
|
|
while (normalized.Contains("//"))
|
|
|
|
|
|
{
|
|
|
|
|
|
normalized = normalized.Replace("//", "/");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return normalized;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|