Path.IsPathRooted メソッド

定義

ファイル パスにルートが含まれているかどうかを示す値を返します。

オーバーロード

IsPathRooted(ReadOnlySpan<Char>)

ファイル パスを表す指定された文字範囲にルートが含まれているかどうかを示す値を返します。

IsPathRooted(String)

指定されたパス文字列にルートが含まれているかどうかを示す値を返します。

注釈

ルート パスは、特定のドライブまたは UNC パスに固定されたファイル パスです。現在のドライブまたは作業ディレクトリに対する相対パスとは対照的です。 たとえば、Windows システムでは、ルート パスは円記号 ("\Documents" など) またはドライブ文字とコロン ("C:Documents" など) で始まります。

ルートパスには、絶対パス (つまり、完全修飾) または相対パスを指定できます。 絶対ルート パスは、ドライブのルートから特定のディレクトリへの完全修飾パスです。 相対ルートパスはドライブを指定しますが、その完全修飾パスは現在のディレクトリに対して解決されます。 この違いを次の例に示します。

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string relative1 = "C:Documents"; 
        ShowPathInfo(relative1);

        string relative2 = "/Documents";
        ShowPathInfo(relative2);

        string absolute = "C:/Documents";
        ShowPathInfo(absolute);
    }

    private static void ShowPathInfo(string path)
    {
        Console.WriteLine($"Path: {path}");
        Console.WriteLine($"   Rooted: {Path.IsPathRooted(path)}");
        Console.WriteLine($"   Fully qualified: {Path.IsPathFullyQualified(path)}");
        Console.WriteLine($"   Full path: {Path.GetFullPath(path)}");
        Console.WriteLine();
    }
}
// The example displays the following output when run on a Windows system:
//    Path: C:Documents
//        Rooted: True
//        Fully qualified: False
//        Full path: c:\Users\user1\Documents\projects\path\ispathrooted\Documents
//
//    Path: /Documents
//       Rooted: True
//       Fully qualified: False
//       Full path: c:\Documents
//
//    Path: C:/Documents
//       Rooted: True
//       Fully qualified: True
//       Full path: C:\Documents
Imports System.IO

Module Program
    Public Sub Main()
        Dim relative1 As String = "C:Documents" 
        ShowPathInfo(relative1)

        Dim relative2 As String = "C:Documents" 
        ShowPathInfo(relative2)

        Dim absolute As String = "C:/Documents"
        ShowPathInfo(absolute)
    End Sub

    Private Sub ShowPathInfo(filepath As String)
        Console.WriteLine($"Path: {filepath}")
        Console.WriteLine($"   Rooted: {Path.IsPathRooted(filepath)}")
        Console.WriteLine($"   Fully qualified: {Path.IsPathFullyQualified(filepath)}")
        Console.WriteLine($"   Full path: {Path.GetFullPath(filepath)}")
        Console.WriteLine()
    End Sub
End Module
' The example displays the following output when run on a Windows system:
'    Path: C:Documents
'        Rooted: True
'        Fully qualified: False
'        Full path: c:\Users\user1\Documents\projects\path\ispathrooted\Documents
'
'    Path: /Documents
'       Rooted: True
'       Fully qualified: False
'       Full path: c:\Documents
'
'    Path: C:/Documents
'       Rooted: True
'       Fully qualified: True
'       Full path: C:\Documents

IsPathRooted(ReadOnlySpan<Char>)

ソース:
Path.Unix.cs
ソース:
Path.Unix.cs
ソース:
Path.Unix.cs

ファイル パスを表す指定された文字範囲にルートが含まれているかどうかを示す値を返します。

public:
 static bool IsPathRooted(ReadOnlySpan<char> path);
public static bool IsPathRooted (ReadOnlySpan<char> path);
static member IsPathRooted : ReadOnlySpan<char> -> bool
Public Shared Function IsPathRooted (path As ReadOnlySpan(Of Char)) As Boolean

パラメーター

path
ReadOnlySpan<Char>

テストするパス。

戻り値

path にルートが含まれている場合は true。それ以外の場合は false

こちらもご覧ください

適用対象

IsPathRooted(String)

ソース:
Path.Unix.cs
ソース:
Path.Unix.cs
ソース:
Path.Unix.cs

指定されたパス文字列にルートが含まれているかどうかを示す値を返します。

public:
 static bool IsPathRooted(System::String ^ path);
public static bool IsPathRooted (string path);
public static bool IsPathRooted (string? path);
static member IsPathRooted : string -> bool
Public Shared Function IsPathRooted (path As String) As Boolean

パラメーター

path
String

テストするパス。

戻り値

path にルートが含まれている場合は true。それ以外の場合は false

例外

.NET Framework バージョンと .NET Core バージョンが 2.1 より前の場合: pathGetInvalidPathChars()定義されている無効な文字が 1 つ以上含まれています。

次の例では、 メソッドを IsPathRooted 使用して 3 つの文字列をテストする方法を示します。

String^ fileName = "C:\\mydir\\myfile.ext";
String^ UncPath = "\\\\myPc\\mydir\\myfile";
String^ relativePath = "mydir\\sudir\\";
bool result;
result = Path::IsPathRooted( fileName );
Console::WriteLine( "IsPathRooted('{0}') returns {1}", fileName, result.ToString() );
result = Path::IsPathRooted( UncPath );
Console::WriteLine( "IsPathRooted('{0}') returns {1}", UncPath, result.ToString() );
result = Path::IsPathRooted( relativePath );
Console::WriteLine( "IsPathRooted('{0}') returns {1}", relativePath, result.ToString() );

// This code produces output similar to the following:
//
// IsPathRooted('C:\mydir\myfile.ext') returns True
// IsPathRooted('\\myPc\mydir\myfile') returns True
// IsPathRooted('mydir\sudir\') returns False
string fileName = @"C:\mydir\myfile.ext";
string UncPath = @"\\myPc\mydir\myfile";
string relativePath = @"mydir\sudir\";
bool result;

result = Path.IsPathRooted(fileName);
Console.WriteLine("IsPathRooted('{0}') returns {1}",
    fileName, result);

result = Path.IsPathRooted(UncPath);
Console.WriteLine("IsPathRooted('{0}') returns {1}",
    UncPath, result);

result = Path.IsPathRooted(relativePath);
Console.WriteLine("IsPathRooted('{0}') returns {1}",
    relativePath, result);

// This code produces output similar to the following:
//
// IsPathRooted('C:\mydir\myfile.ext') returns True
// IsPathRooted('\\myPc\mydir\myfile') returns True
// IsPathRooted('mydir\sudir\') returns False
Dim fileName As String = "C:\mydir\myfile.ext"
Dim UncPath As String = "\\myPc\mydir\myfile"
Dim relativePath As String = "mydir\sudir\"
Dim result As Boolean

result = Path.IsPathRooted(fileName)
Console.WriteLine("IsPathRooted('{0}') returns {1}", fileName, result)

result = Path.IsPathRooted(UncPath)
Console.WriteLine("IsPathRooted('{0}') returns {1}", UncPath, result)

result = Path.IsPathRooted(relativePath)
Console.WriteLine("IsPathRooted('{0}') returns {1}", relativePath, result)

' This code produces output similar to the following:
'
' IsPathRooted('C:\mydir\myfile.ext') returns True
' IsPathRooted('\\myPc\mydir\myfile') returns True
' IsPathRooted('mydir\sudir\') returns False

注釈

メソッドは IsPathRooted 、最初の文字が "\" などのディレクトリ区切り文字である場合、またはパスがドライブ文字とコロン (:)で始まる場合に を返 true します。 たとえば、"\\MyDir\MyFile.txt"、"C:\MyDir"、"C:MyDir" などの文字列を返truepathします。 "MyDir" などの文字列に対して path が返falseされます。

このメソッドでは、パスまたはファイル名が存在するかどうかは確認されません。

共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。

こちらもご覧ください

適用対象