1

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.

Sam
  • 602
  • 9
  • 21
  • Pedantic alert: `\\?\UNC\server\share` is also a [valid UNC path](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx). – Alyssa Haroldsen May 05 '16 at 21:04
  • Also, are you using the mathematical definition of permutations? Are you sure you don't mean path components? – Alyssa Haroldsen May 05 '16 at 21:15
  • 1
    `Path.HasExtension(@"PRTK 7.0.0\KEYLOK 1.6")` returns `true`. You will probably have to use [this](http://stackoverflow.com/a/1395226/5095502) to disambiguate between a directory with a `.` in it and a file that has an extension. – Quantic May 05 '16 at 21:19

0 Answers0