给定一个路径,我需要知道它是否是一个完全限定的路径(绝对路径)。
我知道有一个名为System.IO.Path.IsPathRooted的方法,但是对于这样的路径,这会返回true:
我已经看到了一个名为IsPathFullyQualified的方法,我对它感兴趣,参见这里,但不幸的是,它似乎在.NET Framework4.5中没有得到认可。那么,对于.NET 4.5有什么等效的方法吗?
发布于 2020-02-23 21:24:27
完全免责声明:--我没有亲自编写这段代码,也没有对它拥有任何权利。这是基于微软的.NET核心源代码。下面是更多的信息。
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
Private 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
Private Shared Function IsDirectorySeparator(c As Char) As Boolean
Return c = Path.DirectorySeparatorChar OrElse c = Path.AltDirectorySeparatorChar
End Function
Private 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核心源浏览器中提取出来的,它适合于使用.NET框架,并转换为VB.NET。
.NET核心的方法使用一个名为IsPartiallyQualified()的内部方法来检查路径是否完全限定(而不是部分限定)。这种内部方法对于不同的操作系统有不同的逻辑。这是上述代码所基于的Windows版本。
用法:
Console.WriteLine(PathEx.IsPathFullyQualified("C:Documents")) ' False
Console.WriteLine(PathEx.IsPathFullyQualified("/Documents")) ' False
Console.WriteLine(PathEx.IsPathFullyQualified("C:\Documents")) ' True发布于 2021-09-08 19:56:28
我无意中看到了这个方法,但意识到其他的答案太复杂了。
我看了MS的源代码:https://source.dot.net/#System.Private.CoreLib/Path.cs,3b4bff90471c3a68
在这里:https://referencesource.microsoft.com/#mscorlib/system/io/path.cs,807960f08fca497d
我注意到的是,在第二个链接中,检查IsPathRooted的方法之一是检查以下内容:(length >= 2 && path[1] == VolumeSeparatorChar)。我只是把这个检查修改成了一个充实的方法。
.Net源代码(链接1)本质上是IsPathFullyQualified => !IsPathRooted,我认为这还不足以进行正确的检查。这是我的替代品。
public static bool IsPathFullyQualified(string path)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
return path.Length >= 3 && path[1] == System.IO.Path.VolumeSeparatorChar && ( path[2] == System.IO.Path.DirectorySeparatorChar | path[2] == System.IO.Path.AltDirectorySeparatorChar );
}编辑:我意识到上面这个简短的方法不接受UNC路径。因此,更新的版本(诚然,它只是通过删除IsPartiallyQualified方法来缩短原来答案的一个版本)如下:
public static bool IsPathFullyQualified(string path)
{
if (path == null) throw new ArgumentNullException(nameof(path));
if (path.Length < 2) return false; //There is no way to specify a fixed path with one character (or less).
if (path.Length == 2 && IsValidDriveChar(path[0]) && path[1] == System.IO.Path.VolumeSeparatorChar) return true; //Drive Root C:
if (path.Length >= 3 && IsValidDriveChar(path[0]) && path[1] == System.IO.Path.VolumeSeparatorChar && IsDirectorySeperator(path[2])) return true; //Check for standard paths. C:\
if (path.Length >= 3 && IsDirectorySeperator(path[0]) && IsDirectorySeperator(path[1])) return true; //This is start of a UNC path
return false; //Default
}
private static bool IsDirectorySeperator(char c) => c == System.IO.Path.DirectorySeparatorChar | c == System.IO.Path.AltDirectorySeparatorChar;
private static bool IsValidDriveChar(char c) => c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z';https://stackoverflow.com/questions/60365892
复制相似问题