58 lines
1.8 KiB
C#
58 lines
1.8 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace Logof_Client;
|
|
|
|
public static class PathUtilities
|
|
{
|
|
private static readonly char[] WindowsInvalidFileNameChars =
|
|
{
|
|
'<', '>', ':', '"', '/', '\\', '|', '?', '*'
|
|
};
|
|
|
|
private static readonly string[] WindowsReservedFileNames =
|
|
{
|
|
"CON", "PRN", "AUX", "NUL",
|
|
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
|
|
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"
|
|
};
|
|
|
|
public static string NormalizeFileSystemPath(string path)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(path)) return path;
|
|
|
|
if (Uri.TryCreate(path, UriKind.Absolute, out var uri) && uri.IsFile)
|
|
return uri.LocalPath;
|
|
|
|
return path;
|
|
}
|
|
|
|
public static string NormalizeFileSystemPath(Uri path)
|
|
{
|
|
return path.IsFile ? path.LocalPath : path.ToString();
|
|
}
|
|
|
|
public static bool HasInvalidFileNameChars(string fileName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(fileName)) return true;
|
|
if (fileName.EndsWith(' ') || fileName.EndsWith('.')) return true;
|
|
if (fileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) return true;
|
|
if (fileName.IndexOfAny(WindowsInvalidFileNameChars) >= 0) return true;
|
|
|
|
var nameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
|
|
foreach (var reservedName in WindowsReservedFileNames)
|
|
{
|
|
if (string.Equals(fileName, reservedName, StringComparison.OrdinalIgnoreCase) ||
|
|
string.Equals(nameWithoutExtension, reservedName, StringComparison.OrdinalIgnoreCase))
|
|
return true;
|
|
}
|
|
|
|
foreach (var c in fileName)
|
|
{
|
|
if (char.IsControl(c)) return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|