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

Wrong Path returned by DirectoryInfo.FullName #63892

Closed
Unknown6656 opened this issue Jan 17, 2022 · 3 comments
Closed

Wrong Path returned by DirectoryInfo.FullName #63892

Unknown6656 opened this issue Jan 17, 2022 · 3 comments
Labels
area-System.IO untriaged New issue has not been triaged by the area owner

Comments

@Unknown6656
Copy link

Unknown6656 commented Jan 17, 2022

Description

I have an application which contains the following code line:

string foo = new DirectoryInfo("c:").FullName;

The string "c:" is user-provided and may be a longer path. If the user passes a path consisting only of the drive label and the colon (while omitting the finishing forward or backward slash) and the drive is the one matching the drive of the current execution directory, then the execution directory is returned instead of the root drive.

Reproduction Steps

  1. Use a machine with multiple drives (if you do not have any explorer-mapped devices/drives, you may use the subst cmd command.
  2. Create the following program:
using System.IO;
using System;


Console.WriteLine("current dir: " + Directory.GetCurrentDirectory());

foreach (string path in new[] {
    "d:",
    "d:/",
    "d:\\",
    "c:",
    "c:/",
    "c:\\",
})
    Console.WriteLine($"    {path,5}  --->  {new DirectoryInfo(path).FullName}");
  1. compile it with net6.0-windows. I use the following version:
C:\> dotnet --list-sdks
3.1.416 [C:\Program Files\dotnet\sdk]
6.0.200-preview.21617.4 [C:\Program Files\dotnet\sdk]
  1. Run it on the main system drive (i.e. c:/).

Expected output

current dir: C:\Users\xxxxx\Documents\TestProject1\bin
       d:  --->  D:\
      d:/  --->  d:\
      d:\  --->  d:\
       c:  --->  C:\
      c:/  --->  c:\
      c:\  --->  c:\

Actual output

current dir: C:\Users\xxxxx\Documents\TestProject1\bin
       d:  --->  D:\
      d:/  --->  d:\
      d:\  --->  d:\
       c:  --->  C:\Users\xxxxx\Documents\TestProject1\bin
      c:/  --->  c:\
      c:\  --->  c:\

Regression?

I do not think that this bug occured in a previous version.

Known Workarounds

I could check whether the path exactly matches ^[a-z]:$ and append a / if that is the case.

Configuration

I am using .NET 6.0.200-preview.21617.4 on Windows 11 Pro 64Bit:

image

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.IO untriaged New issue has not been triaged by the area owner labels Jan 17, 2022
@ghost
Copy link

ghost commented Jan 17, 2022

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

I have an application which contains the following code line:

string foo = new DirectoryInfo("c:").FullName;

The string "c:" is user-provided and may be a longer path. If the user passes a path consisting only of the drive label and the colon (while omitting the finishing forward or backward slash) and the drive is the one matching the drive of the current execution directory, then the execution directory is returned instead of the root drive.

Reproduction Steps

  1. Use a machine with multiple drives (if you do not have any explorer-mapped devices/drives, you may use the subst cmd command.
  2. Create the following program:
using System.IO;
using System;


Console.WriteLine("current dir: " + Directory.GetCurrentDirectory());

foreach (string path in new[] {
    "d:",
    "d:/",
    "d:\\",
    "c:",
    "c:/",
    "c:\\",
})
    Console.WriteLine($"    {path,5}  --->  {new DirectoryInfo(path).FullName}");
  1. compile it with net6.0-windows. I use the following version:
C:\> dotnet --list-sdks
3.1.416 [C:\Program Files\dotnet\sdk]
6.0.200-preview.21617.4 [C:\Program Files\dotnet\sdk]
  1. Run it on the main system drive (i.e. c:/).

Expected behavior

Expected output:

current dir: C:\Users\xxxxx\Documents\TestProject1\bin
       d:  --->  D:\
      d:/  --->  d:\
      d:\  --->  d:\
       c:  ---> C:\
      c:/  --->  c:\
      c:\  --->  c:\

Actual behavior

Actual output:

current dir: C:\Users\xxxxx\Documents\TestProject1\bin
       d:  --->  D:\
      d:/  --->  d:\
      d:\  --->  d:\
       c:  ---> C:\Users\xxxxx\Documents\TestProject1\bin
      c:/  --->  c:\
      c:\  --->  c:\

Regression?

I do not think that this bug occured in a previous version.

Known Workarounds

I could check whether the path exactly matches ^[a-z]:$ and append a / if that is the case.

Configuration

I am using .NET 6.0.200-preview.21617.4 on Windows 11 Pro 64Bit:

image

Other information

No response

Author: Unknown6656
Assignees: -
Labels:

area-System.IO, untriaged

Milestone: -

@danmoseley
Copy link
Member

Hello @Unknown6656 this seems to be original behavior:

C:\proj\9>dotnet run -f net6.0
current dir: C:\proj\9
       d:  --->  D:\
      d:/  --->  d:\
      d:\  --->  d:\
       c:  --->  C:\proj\9
      c:/  --->  c:\
      c:\  --->  c:\

C:\proj\9>dotnet run -f net48
current dir: C:\proj\9
       d:  --->  D:\
      d:/  --->  d:\
      d:\  --->  d:\
       c:  --->  C:\proj\9
      c:/  --->  c:\
      c:\  --->  c:\

On Windows, "X:" means "current directory on the X drive". Our implementation ultimately calls GetFullPathName which gives the example "If you specify "U:" the path returned is the current directory on the "U:" drive".

If you want the behavior you describe, your workaround sounds like a good one.

thanks for opening the issue!

@Unknown6656
Copy link
Author

Hello @danmoseley,
Thank you for replying so quickly. I was not aware that this behaviour was intended. Thanks for taking your time in order to clear up the issue. I must admit that the behaviour of resolving "x:" to the current path is a bit off-putting, but thanks anyways!

@ghost ghost locked as resolved and limited conversation to collaborators Feb 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.IO untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants