Path.GetPathRoot メソッド

定義

オーバーロード

GetPathRoot(String)

指定した文字列に含まれるパスから、ルート ディレクトリ情報を取得します。

GetPathRoot(ReadOnlySpan<Char>)

指定した文字範囲に含まれるパスから、ルート ディレクトリ情報を取得します。

GetPathRoot(String)

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

指定した文字列に含まれるパスから、ルート ディレクトリ情報を取得します。

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

パラメーター

path
String

ルート ディレクトリ情報の取得元のパスを含む文字列。

戻り値

ルートである場合は path のルート ディレクトリ。

または

path にルート ディレクトリ情報が含まれていない場合は Empty

または

pathnull の場合、またはが実質的に空の場合は null

例外

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

または

.NET Framework のみ: Emptypath に渡されました。

次の例では、 メソッドの使用方法を GetPathRoot 示します。

String^ path = "\\mydir\\";
String^ fileName = "myfile.ext";
String^ fullPath = "C:\\mydir\\myfile.ext";
String^ pathRoot;
pathRoot = Path::GetPathRoot( path );
Console::WriteLine( "GetPathRoot('{0}') returns '{1}'", path, pathRoot );
pathRoot = Path::GetPathRoot( fileName );
Console::WriteLine( "GetPathRoot('{0}') returns '{1}'", fileName, pathRoot );
pathRoot = Path::GetPathRoot( fullPath );
Console::WriteLine( "GetPathRoot('{0}') returns '{1}'", fullPath, pathRoot );

// This code produces output similar to the following:
//
// GetPathRoot('\mydir\') returns '\'
// GetPathRoot('myfile.ext') returns ''
// GetPathRoot('C:\mydir\myfile.ext') returns 'C:\'
string path = @"\mydir\";
string fileName = "myfile.ext";
string fullPath = @"C:\mydir\myfile.ext";
string pathRoot;

pathRoot = Path.GetPathRoot(path);
Console.WriteLine("GetPathRoot('{0}') returns '{1}'",
    path, pathRoot);

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

pathRoot = Path.GetPathRoot(fullPath);
Console.WriteLine("GetPathRoot('{0}') returns '{1}'",
    fullPath, pathRoot);

// This code produces output similar to the following:
//
// GetPathRoot('\mydir\') returns '\'
// GetPathRoot('myfile.ext') returns ''
// GetPathRoot('C:\mydir\myfile.ext') returns 'C:\'
Dim pathname As String = "\mydir\"
Dim fileName As String = "myfile.ext"
Dim fullPath As String = "C:\mydir\myfile.ext"
Dim pathnameRoot As String

pathnameRoot = Path.GetPathRoot(pathname)
Console.WriteLine("GetPathRoot('{0}') returns '{1}'", pathname, pathnameRoot)

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

pathnameRoot = Path.GetPathRoot(fullPath)
Console.WriteLine("GetPathRoot('{0}') returns '{1}'", fullPath, pathnameRoot)

' This code produces output similar to the following:
'
' GetPathRoot('\mydir\') returns '\'
' GetPathRoot('myfile.ext') returns ''
' GetPathRoot('C:\mydir\myfile.ext') returns 'C:\'

注釈

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

このメソッドは、ディレクトリ区切り記号を正規化します。

次の場合、文字列は "実質的に空" になります。

  • Windows では、この文字列で を呼び出すと IsEmpty が返 trueされるか、そのすべての文字がスペース (' ') になります。
  • Unix では、この文字列で を呼び出すと IsNullOrEmpty が返されます true

このメソッドによって返される文字列のパターンは次のとおりです。

  • null (path が null または空の文字列でした)。

  • 空の文字列 (path 現在のドライブまたはボリュームの相対パスを指定)。

  • "/" (Unix: path 現在のドライブの絶対パスを指定)。

  • "X:" (Windows: path ドライブの相対パスを指定しました。 X はドライブまたはボリューム文字を表します)。

  • "X:\"(Windows: 特定 path のドライブに絶対パスを指定しました)。

  • "\\ComputerName\SharedFolder" (Windows: UNC パス)。

  • "\\?\C:" (Windows: .NET Core 1.1 以降のバージョンと、.NET Framework 4.6.2 以降のバージョンでサポートされている DOS デバイス パス)。

Windows 上のファイル パスの詳細については、「Windows システムのファイル パス形式」を参照してください。 共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。

こちらもご覧ください

適用対象

GetPathRoot(ReadOnlySpan<Char>)

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

指定した文字範囲に含まれるパスから、ルート ディレクトリ情報を取得します。

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

パラメーター

path
ReadOnlySpan<Char>

ルート ディレクトリ情報の取得元のパスを含む文字の読み取り専用スパン。

戻り値

path のルート ディレクトリを含む文字の読み取り専用スパン。

注釈

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

文字列オーバーロードとは異なり、このメソッドはディレクトリ区切り記号を正規化しません。

ReadOnlySpan<System.Char> の場合、 は "実質的に空" です。

  • Windows では、このスパンの文字で を呼び出すと ReadOnlySpan<T>.IsEmpty が返 trueされるか、そのすべての文字がスペース (' ') になります。
  • Unix では、このスパンの文字で を呼び出すと ReadOnlySpan<T>.IsEmpty 、 が返されます true

このメソッドによって返される読み取り専用文字スパンのパターンは次のとおりです。

  • ReadOnlySpan<T>.Empty (path は でした ReadOnlySpan<T>.Empty

  • ReadOnlySpan<T>.Empty (path は、現在のドライブまたはボリュームの相対パスを指定しました)。

  • "/" (Unix: path 現在のドライブの絶対パスを指定)。

  • "X:" (Windows: path ドライブの相対パスを指定しました。 X はドライブまたはボリューム文字を表します)。

  • "X:\"(Windows: 特定 path のドライブに絶対パスを指定しました)。

  • "\\ComputerName\SharedFolder" (Windows: UNC パス)。

  • "\\?\C:" (Windows: .NET Core 1.1 以降のバージョンと、.NET Framework 4.6.2 以降のバージョンでサポートされている DOS デバイス パス)。

Windows 上のファイル パスの詳細については、「Windows システムのファイル パス形式」を参照してください。 共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。

こちらもご覧ください

適用対象