Path.GetDirectoryName() 没有为 .net 核心中的空路径返回 ArgumentException

Path.GetDirectoryName() isn't returning ArgumentException for empty path in .net core

我正在将我的代码从 .Net Framework 迁移到 .Net Core,并且我在 API 中看到一个称为 Path.GetDirectoryName() 的回归。虽然我向它传递了一个空值(即“”),但它没有返回我 System.ArgumentException 而是返回我 null 值。

这过去在 .Net Framework 中运行良好,根据文档,这就是 .Net Core 中的行为方式:

// Exceptions:

// T:System.ArgumentException:

// The path parameter contains invalid characters, is empty, or contains only white // spaces.

来自 corefx System.IO.Path.GetDirectoryName 方法的文档:

/// <summary>
/// Returns the directory portion of a file path. This method effectively
/// removes the last segment of the given file path, i.e. it returns a
/// string consisting of all characters up to but not including the last
/// backslash ("\") in the file path. The returned value is null if the
/// specified path is null, empty, or a root (such as "\", "C:", or
/// "\server\share").
/// </summary>
/// <remarks>
/// Directory separators are normalized in the returned string.
/// </remarks>
public static string GetDirectoryName(string path)
{
    if (path == null || PathInternal.IsEffectivelyEmpty(path.AsSpan()))
        return null;

    int end = GetDirectoryNameOffset(path.AsSpan());
    return end >= 0 ? PathInternal.NormalizeDirectorySeparators(path.Substring(0, end)) : null;
}

所以,它与完整框架版本中的不一样,returns null 如果 指定的路径为 null、空或根。这是预期的行为