Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behavior in Directory.GetDirectories when using long path prefix (\\?\) #2286

Open
nick-beer opened this issue Jan 28, 2020 · 1 comment
Labels
area-System.IO enhancement Product code improvement that does NOT require public API changes/additions
Milestone

Comments

@nick-beer
Copy link
Contributor

While porting to .NET Core, our test suite uncovered a difference in behavior in the Directory.GetDirectories API depending upon whether the long path prefix "\\?\" is used or not. Consider the following code:

using System;
using System.Diagnostics;
using System.IO;

namespace GetDirectories
{
    class Program
    {
        static void Main(string[] args)
        {
            Exception e1 = null, e2 = null;
            string[] results1, results2;
            try
            {
                results1 = Directory.GetDirectories(@"\\?\C:\Program Files", @"..\*");
            }
            catch (Exception e)
            {
                e1 = e;
            }

            try
            {
                results2 = Directory.GetDirectories(@"C:\Program Files", @"..\*");
            }
            catch (Exception e)
            {
                e2 = e;
            }
        }
    }
}

The first call to GetDirectories throws an IOException stating

The filename, directory name, or volume label syntax is incorrect. : '\?\C:\Program Files..'

The second call to GetDirectories returns the expected (admittedly questionably so) directories and does not throw an exception.

In .NET Framework, both calls consistently throw an ArgumentException indicating the search pattern cannot contain ..\ in order to move up directories.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Jan 28, 2020
@JeremyKuhne JeremyKuhne added bug enhancement Product code improvement that does NOT require public API changes/additions and removed untriaged New issue has not been triaged by the area owner bug labels Mar 5, 2020
@JeremyKuhne
Copy link
Member

JeremyKuhne commented Mar 5, 2020

This is expected behavior on Core as we no longer block looking out of directories (i.e. ..). The reason the first one fails is that \\?\ is considered normalized by Windows and .. is not legitimate in a normalized path.

I'm not terribly fond of the arbitrary separation of the path/filter inputs. There is no reason outside of backward compatibility to have anything but a file filter in the searchPattern. The code that handles this is here.

I'm inclined to make a breaking change in 5.0 to no longer support passing path segments in addition to a filter in searchPattern.

@JeremyKuhne JeremyKuhne added this to the 5.0 milestone Mar 5, 2020
@carlossanlop carlossanlop added this to To do in System.IO - File system via automation Mar 6, 2020
@carlossanlop carlossanlop added this to To do in System.IO - Enumeration via automation Mar 6, 2020
@carlossanlop carlossanlop removed this from To do in System.IO - File system Mar 6, 2020
@carlossanlop carlossanlop modified the milestones: 5.0.0, Future Jun 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.IO enhancement Product code improvement that does NOT require public API changes/additions
Projects
Development

No branches or pull requests

5 participants