8

I am running a very simple program, which is trying to list files in a folder on the same machine, which is specified using UNC format(as described in http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx):


static string rootDir = @"\\?\d:\share\input";

static void Main(string[] args)
{
    char[] invlidChars = Path.GetInvalidPathChars();
    foreach (char invChar in invlidChars)
    {
        if (rootDir.Contains(invChar.ToString()))
        {
            Console.WriteLine("InvChar - {0}", invChar);
        }
    }
    string[] matchFiles = Directory.GetFiles(rootDir);
}

However the Directory.GetFiles() does not work and throws an ArgumentException(which is thrown when - path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.). The Console.Writeline() does not print anything, which confirms that there are no invalid chars in the path. I get the same exception when I use "\\UNC\?\d:\share\input" or "\\UNC\?\machinename\share\input" or "\\?\machinename\share\input".

The "d:\share\input" is indeed a shared folder.

Does anybody know what could be wrong?

Thanks!

whywhywhy
  • 278
  • 1
  • 5
  • 15
  • I recommend this old but still relevant overview of long paths, the MAXPATH limitation, \\?\ prefix and other .NET issues: http://blogs.msdn.com/b/bclteam/archive/2007/02/13/long-paths-in-net-part-1-of-3-kim-hamilton.aspx – Avner Shahar-Kashtan Jun 13 '12 at 06:49
  • Thanks for the link Avner S-K; This clarifies the behavior of .NET and Windows-API with long paths. – whywhywhy Jun 13 '12 at 12:53
  • UNC: replace "\\" with "\\?\UNC\" for UNC paths – OzBob Apr 04 '18 at 03:53

1 Answers1

10

The problem is that \\?\ is a windows API convention that is not supported by .NET. If you read carefully in your link \\?\ does not denote a UNC path, but is a special convention for windows API:

For file I/O, the "\\?\" prefix to a path string tells the Windows APIs to disable all string parsing and to send the string that follows it straight to the file system.

A .NET compatible UNC format would be \\machinename\d$\share\input. See this answer for more info.

The reason it is not supported by .NET is most likely that the extended path convention is not available on all platforms and therefore cannot be guaranteed to work by the framework.

Community
  • 1
  • 1
Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • Thanks for the response Mike Z. As you point out indeed \\?\ is not part of UNC but windows specific. This answers the question! – whywhywhy Jun 13 '12 at 12:56
  • yes, https://stackoverflow.com/questions/6867463/is-there-a-net-api-to-return-the-equivalent-extended-path-string-of-a-given-pat has more details. I've added a section about UNC paths and tests. – OzBob Apr 04 '18 at 03:52