mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 11:37:01 +00:00
83 lines
2.3 KiB
C#
83 lines
2.3 KiB
C#
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;
|
||
|
||
// 保留驱动器字母和冒号(如 0:)
|
||
// 检查格式:X:\ 其中 X 是单个字符(字母或数字)
|
||
bool hasDrive = sanitized.Length >= 2 && sanitized[1] == ':';
|
||
string drive = hasDrive ? sanitized.Substring(0, 2) : "";
|
||
string rest = hasDrive ? sanitized.Substring(2) : sanitized;
|
||
|
||
// 不替换反斜杠,因为 Cosmos 文件系统需要反斜杠
|
||
rest = rest.Replace(':', '_');
|
||
rest = rest.Replace('*', '_');
|
||
rest = rest.Replace('?', '_');
|
||
rest = rest.Replace('"', '_');
|
||
rest = rest.Replace('<', '_');
|
||
rest = rest.Replace('>', '_');
|
||
rest = rest.Replace('|', '_');
|
||
|
||
// 只移除结尾的斜杠,保留开头的斜杠(因为驱动器后面需要斜杠)
|
||
rest = rest.TrimEnd('/');
|
||
rest = rest.TrimEnd('\\');
|
||
|
||
return drive + rest;
|
||
}
|
||
|
||
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;
|
||
}
|
||
}
|
||
}
|