IOUtils Changes

- added new NormalizeFolder method to format the path parameter, so that it is formed correctly.
If the path is a local root path (eg. d:) then it must end with \\.
If the path is not a root path, or is a Unc path, then any ending \\ must be removed.

This fix was due to an issue with the previous code, as it kept appending a \ to the end when the path was a Unc path.
This commit is contained in:
Brett Hewitson 2022-05-09 00:25:27 +10:00
parent 14cff018cd
commit bbaad6099f
9 changed files with 210 additions and 222 deletions

View file

@ -10,11 +10,37 @@ namespace ServerManagerTool.Common.Utils
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeleteFile(string name);
public static bool IsUnc(string path) => new Uri(path).IsUnc;
public static string NormalizeFolder(string path)
{
var newPath = path.TrimEnd('\\') + "\\";
if (IsUnc(newPath))
{
return newPath.TrimEnd('\\');
}
var root = Path.GetPathRoot(newPath);
if (!root.EndsWith("\\"))
root += "\\";
if (!string.Equals(root, newPath, StringComparison.OrdinalIgnoreCase))
{
newPath = newPath.TrimEnd('\\');
}
return newPath;
}
public static string NormalizePath(string path) =>
Path.GetFullPath(new Uri(path).LocalPath)
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
.ToLowerInvariant();
public static bool Unblock(string fileName)
{
return DeleteFile(fileName + ":Zone.Identifier");
}
public static string NormalizePath(string path) => Path.GetFullPath(new Uri(path).LocalPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).ToLowerInvariant();
}
}