Powershell: System.IO.Path.GetDirectoryName does not throw in PowerShell when passed in null

Created on 10 Oct 2019  路  3Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

In PowerShell 5.1, the following logic produces an error. In PowerShell 6+, it does not.

try {[System.IO.Path]::GetDirectoryName($null)} catch {throw}

Is this a bug in PowerShell, or .NET?

Expected behavior

An exception should be thrown.

Actual behavior

No exception is thrown.

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0-preview.4
PSEdition                      Core
GitCommitId                    7.0.0-preview.4
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Issue-Question Resolution-Answered

All 3 comments

The framework behavior must have changed: .NET Framework throws an error with '' (which is what the PowerShell $null argument is converted to - unless you use [NullString]::Value) - whereas CoreFX now treats the empty string the same as null (don't know if that was an intentional change or not).

# $True in .NET Core, exception in .NET Framework.
PS> Add-type -MemberDefinition 'public static bool Test() { return null == System.IO.Path.GetDirectoryName(""); }' -Name Tst -Namespace Tst; [Tst.Tst]::Test()
True

Thanks for confirming. I've logged the issue in the .NET Core repository, since it seems to be either a documentation problem (docs need to be updated to reflect change) or a regression.

Thanks @mklement0 I just dug into that repo and found out that the changes are intentional, but the docs haven't been updated to reflect those changes yet (still open issue for that), so I closed the other issue I logged. Nothing more to do here.

Was this page helpful?
0 / 5 - 0 ratings