I am trying to find all possible permutations of a given file path. This is required to create some directories on a remote server, before transferring some files.
This is my code;
public class PathUtils
{
public bool IsUNC(String path)
{
string root = Path.GetPathRoot(path);
return root.StartsWith(new String(Path.DirectorySeparatorChar, 2));
}
public void Combinations(HashSet<String> paths, String path)
{
if (!Path.HasExtension(path) && !path.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
path = path + Path.DirectorySeparatorChar;
}
if (IsUNC(path))
{
paths.Add(new String(Path.DirectorySeparatorChar, 2) + (new Uri(path)).Host);
}
String previous = path;
while (!String.IsNullOrEmpty(Path.GetDirectoryName(previous)))
{
paths.Add(Path.GetDirectoryName(previous));
previous = Path.GetDirectoryName(previous);
}
paths.Add(Path.GetPathRoot(path));
}
}
However, when I give it a path
of PRTK 7.0.0\KEYLOK 1.6
, the only value that is returned in the HashSet is PRTK 7.0.0
when there should be two, PRTK 7.0.0
and KEYLOK 1.6
. If I provide a path of PRTK 7.0.0\KEYLOK 16
, I get the expected result, two items in the hashset - PRTK 7.0.0
and KEYLOK 16
.
I'm unsure of why this behavior is happening, and any help is appreciated.