Skip to content

Suggestion: Rename System.IO.Path.GetDirectoryName to System.IO.Path.GetDirectoryPath for clarity #115965

Closed as not planned
@cos4ni2s

Description

@cos4ni2s

Description

Description:

The naming of the [System.IO.Path]::GetDirectoryName() method can be somewhat misleading when compared to the behavior of [System.IO.Path]::GetFileName().

  • GetFileName() correctly returns only the name (and extension) of the file from a given path. For example, given "C:\path\to\file.txt", it returns "file.txt".
  • GetDirectoryName(), however, returns the full path to the parent directory. For example, given "C:\path\to\folder\file.txt", it returns "C:\path\to\folder".

This inconsistency in naming, where "Name" in GetDirectoryName() actually refers to a path, can be counter-intuitive and potentially lead to confusion for developers. One might reasonably expect GetDirectoryName() to return just the name of the directory (e.g., "folder" in the example above) to align with the behavior of GetFileName().

Proposed Solution:

To improve clarity and consistency within the System.IO.Path API, it is suggested to rename the GetDirectoryName() method to GetDirectoryPath(). This new name would accurately reflect the method's current behavior of returning the full path to the parent directory.

Reproduction Steps

  1. Execute the following command to get the file name from a sample path:
    [System.IO.Path]::GetFileName("C:\temp\myfile.txt")
    Observe that it returns only "myfile.txt".
  2. Execute the following command to get the directory name from a sample path:
    [System.IO.Path]::GetDirectoryName("C:\temp\mydirectory\myfile.txt")
    Observe that it returns "C:\temp\mydirectory", which is the full path to the parent directory, not just the directory's name ("mydirectory").

This demonstrates the inconsistency where GetFileName() returns a name, but GetDirectoryName() returns a path.

Expected behavior

[System.IO.Path]::GetDirectoryName("C:\temp\mydirectory\myfile.txt") should return mydirectory

Actual behavior

[System.IO.Path]::GetDirectoryName("C:\temp\mydirectory\myfile.txt") returns C:\temp\mydirectory

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions