如何确定路径是否完全合格?

问题描述 投票:1回答:2

给定一条路径,我需要知道它是否是完全限定的路径(绝对路径)。

我知道有一个称为System.IO.Path.IsPathRooted的方法,但对于类似这样的路径,它返回true:

  • C:Documents
  • /文档

我已经看到了一个我感兴趣的名为IsPathFullyQualified的方法,请参阅here,但不幸的是,它似乎在.NET Framework 4.5中无法识别。那么,.NET 4.5是否有任何等效方法?

.net vb.net .net-4.5
2个回答
0
投票

完全免责声明:我自己没有编写此代码,也不拥有任何权利。这基于Microsoft的.NET Core源。下面的更多信息。

TL; DR:对于Windows系统,可以将以下类添加到项目中,然后调用PathEx.IsPathFullyQualified()检查路径是否完全合格:

Public Class PathEx
    Public Shared Function IsPathFullyQualified(path As String) As Boolean
        If path Is Nothing Then
            Throw New ArgumentNullException(NameOf(path))
        End If

        Return Not IsPartiallyQualified(path)
    End Function

    Friend Shared Function IsPartiallyQualified(path As String) As Boolean
        If path.Length < 2 Then
            ' It isn't fixed, it must be relative.  There is no way to specify a fixed
            ' path with one character (or less).
            Return True
        End If

        If IsDirectorySeparator(path.Chars(0)) Then
            ' There is no valid way to specify a relative path with two initial slashes or
            ' \? as ? isn't valid for drive relative paths and \??\ is equivalent to \\?\
            Return Not (path.Chars(1) = "?"c OrElse IsDirectorySeparator(path.Chars(1)))
        End If

        ' The only way to specify a fixed path that doesn't begin with two slashes
        ' is the drive, colon, slash format- i.e. C:\
        Return Not ((path.Length >= 3) AndAlso
                    (path.Chars(1) = IO.Path.VolumeSeparatorChar) AndAlso
                    IsDirectorySeparator(path.Chars(2)) AndAlso
                    IsValidDriveChar(path.Chars(0)))
        '           ^^^^^^^^^^^^^^^^
        ' To match old behavior we'll check the drive character for validity as
        ' the path is technically not qualified if you don't have a valid drive.
        ' "=:\" is the "=" file's default data stream.
    End Function

    Friend Shared Function IsDirectorySeparator(c As Char) As Boolean
        Return c = Path.DirectorySeparatorChar OrElse c = Path.AltDirectorySeparatorChar
    End Function

    Friend Shared Function IsValidDriveChar(value As Char) As Boolean
        Return (value >= "A"c AndAlso value <= "Z"c) OrElse
               (value >= "a"c AndAlso value <= "z"c)
    End Function
End Class

此代码(和注释)从哪里来?

这是从.NET Core Source Browser中提取的,该IsPathFullyQualified() method是公开可用的,适用于.NET Framework,并转换为VB.NET。

。NET Core的IsPathFullyQualified()使用称为IsPartiallyQualified()的内部方法来检查路径是否完全合格(不是部分合格)。对于不同的操作系统,该内部方法具有不同的逻辑。 This是上述代码基于的Windows版本。

用法:

Console.WriteLine(PathEx.IsPathFullyQualified("C:Documents"))    ' False
Console.WriteLine(PathEx.IsPathFullyQualified("/Documents"))     ' False
Console.WriteLine(PathEx.IsPathFullyQualified("C:\Documents"))   ' True

0
投票

这里是确定路径是否完全合格且也有效的另一种可能方法。此方法尝试使用Uri.TryCreate()从提供的路径生成Uri。成功执行此方法后,它将检查非公开的IsDosPath属性,该属性在解析新的Uri时由Uri类在内部设置,以确定其是否有效以及代表的资源类型。

您可以在.Net Source code中看到PrivateParseMinimal()方法执行了许多测试以验证路径,还检查了DosPath是否已扎根。

Private Function PathIsFullyQualified(path As String) As (Valid As Boolean, Parsed As String)
    Dim flags = BindingFlags.GetProperty Or BindingFlags.Instance Or BindingFlags.NonPublic
    Dim uri As Uri = Nothing
    If Uri.TryCreate(path, UriKind.Absolute, uri) Then
        Dim isDosPath = CBool(uri.GetType().GetProperty("IsDosPath", flags).GetValue(uri))
        Return (isDosPath, uri.LocalPath)
    End If
    Return (False, String.Empty)
End Function

我测试了以下路径;除了False:以外,它们都返回"file://c:/Documents",但PathIsFullyQualified方法还返回相应的本地路径c:\Documents

Dim isOk1 = PathIsFullyQualified("C:Documents")            'False
Dim isOk2 = PathIsFullyQualified("/Documents")             'False
Dim isOk3 = PathIsFullyQualified("file://c:/Documents")    'True  => isOk3.Parsed = "c:\Documents"
Dim isOk4 = PathIsFullyQualified("\\Documents")            'False
Dim isOk5 = PathIsFullyQualified("..\Documents")           'False
Dim isOk6 = PathIsFullyQualified(".\Documents")            'False
Dim isOk7 = PathIsFullyQualified("\Documents")             'False
Dim isOk8 = PathIsFullyQualified("//Documents")            'False
Dim isOk9 = PathIsFullyQualified(".Documents")             'False
Dim isOkA = PathIsFullyQualified("..Documents")            'False
Dim isOkB = PathIsFullyQualified("http://C:/Documents")    'False
Dim isOkC = PathIsFullyQualified("Cd:\Documents")          'False
Dim isOkD = PathIsFullyQualified("1:\Documents")           'False
Dim isOkE = PathIsFullyQualified("Z:\\Another Path//docs") 'True => isOkE.Parsed = "Z:\Another Path\docs"
Dim isOkF = PathIsFullyQualified(":\\Another Path//docs")  'False
© www.soinside.com 2019 - 2024. All rights reserved.