Bug fix: in paths, double slashes are not allowed *except* at beginning. eg: \\192.168.0.1 (issue #157 )
This commit is contained in:
parent
b3dc5a7054
commit
530b44a0e6
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
<Version>6.5.0.1</Version>
|
<Version>6.5.1.1</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@ -86,34 +86,54 @@ namespace FileManager
|
|||||||
{
|
{
|
||||||
ArgumentValidator.EnsureNotNull(path, nameof(path));
|
ArgumentValidator.EnsureNotNull(path, nameof(path));
|
||||||
|
|
||||||
var invalidChars = Path.GetInvalidPathChars().Union(new[] {
|
path = replaceInvalidChars(path, illegalCharacterReplacements);
|
||||||
|
path = standardizeSlashes(path);
|
||||||
|
path = replaceColons(path, illegalCharacterReplacements);
|
||||||
|
path = removeDoubleSlashes(path);
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static char[] invalidChars { get; } = Path.GetInvalidPathChars().Union(new[] {
|
||||||
'*', '?',
|
'*', '?',
|
||||||
// these are weird. If you run Path.GetInvalidPathChars() in C# interactive, these characters are included.
|
// these are weird. If you run Path.GetInvalidPathChars() in Visual Studio's "C# Interactive", then these characters are included.
|
||||||
// In live code, Path.GetInvalidPathChars() does not include them
|
// In live code, Path.GetInvalidPathChars() does not include them
|
||||||
'"', '<', '>'
|
'"', '<', '>'
|
||||||
}).ToArray();
|
}).ToArray();
|
||||||
|
private static string replaceInvalidChars(string path, string illegalCharacterReplacements)
|
||||||
|
=> string.Join(illegalCharacterReplacements ?? "", path.Split(invalidChars));
|
||||||
|
|
||||||
var fixedPath = string
|
private static string standardizeSlashes(string path)
|
||||||
.Join(illegalCharacterReplacements ?? "", path.Split(invalidChars))
|
=> path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
|
||||||
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
|
|
||||||
|
|
||||||
|
private static string replaceColons(string path, string illegalCharacterReplacements)
|
||||||
|
{
|
||||||
// replace all colons except within the first 2 chars
|
// replace all colons except within the first 2 chars
|
||||||
var builder = new System.Text.StringBuilder();
|
var builder = new System.Text.StringBuilder();
|
||||||
for (var i = 0; i < fixedPath.Length; i++)
|
for (var i = 0; i < path.Length; i++)
|
||||||
{
|
{
|
||||||
var c = fixedPath[i];
|
var c = path[i];
|
||||||
if (i >= 2 && c == ':')
|
if (i >= 2 && c == ':')
|
||||||
builder.Append(illegalCharacterReplacements);
|
builder.Append(illegalCharacterReplacements);
|
||||||
else
|
else
|
||||||
builder.Append(c);
|
builder.Append(c);
|
||||||
}
|
}
|
||||||
fixedPath = builder.ToString();
|
return builder.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string removeDoubleSlashes(string path)
|
||||||
|
{
|
||||||
|
if (path.Length < 2)
|
||||||
|
return path;
|
||||||
|
|
||||||
|
// exception: don't try to condense the initial dbl bk slashes in a path. eg: \\192.168.0.1
|
||||||
|
|
||||||
|
var remainder = path[1..];
|
||||||
var dblSeparator = $"{Path.DirectorySeparatorChar}{Path.DirectorySeparatorChar}";
|
var dblSeparator = $"{Path.DirectorySeparatorChar}{Path.DirectorySeparatorChar}";
|
||||||
while (fixedPath.Contains(dblSeparator))
|
while (remainder.Contains(dblSeparator))
|
||||||
fixedPath = fixedPath.Replace(dblSeparator, $"{Path.DirectorySeparatorChar}");
|
remainder = remainder.Replace(dblSeparator, $"{Path.DirectorySeparatorChar}");
|
||||||
|
|
||||||
return fixedPath;
|
return path[0] + remainder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user