次の方法で共有


Path.GetPathRoot メソッド

指定したパスのルート ディレクトリ情報を取得します。

Public Shared Function GetPathRoot( _
   ByVal path As String _) As String
[C#]
public static string GetPathRoot(stringpath);
[C++]
public: static String* GetPathRoot(String* path);
[JScript]
public static function GetPathRoot(
   path : String) : String;

パラメータ

  • path
    ルート ディレクトリ情報を取得する対象のパス。

戻り値

path にルート ディレクトリ情報が含まれている場合は、"C:\" などのルート ディレクトリを含む文字列、 path が null 参照 (Visual Basic では Nothing) の場合は null 参照 (Nothing) 、 path にルート ディレクトリ情報が含まれていない場合には空の文字列。

例外

例外の種類 条件
ArgumentException path に、 InvalidPathChars で定義されている無効な文字が 1 つ以上含まれています。

または

String.Emptypath に渡されました。

解説

.NET Framework では、"\\.\PHYSICALDRIVE0" など、デバイス名を指定したパスを使用して物理ディスクに直接アクセスすることはできません。

このメソッドは、パス名またはファイル名が存在することを検査しません。

このメソッドで返される、有効な文字列のパターンは次のとおりです。

  • 空の文字列 (path で現在のドライブまたはボリュームに対する相対パスを指定しました)。
  • "/" (path で現在のドライブに対する絶対パスを指定しました)。
  • "X:" (path は、ドライブで相対パスを指定しました。X はドライブ名またはボリューム名を表します)。
  • "X:/" (path は指定したドライブで絶対パスを指定しました)。

このメソッドの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。

実行するタスク 参考例があるトピック
テキスト ファイルを作成する。 ファイルへのテキストの書き込み
テキスト ファイルに書き込む。 ファイルへのテキストの書き込み
テキスト ファイルから読み取る。 ファイルからのテキストの読み取り
ファイルの拡張子を取得する。 GetExtension
ファイルの絶対パスを取得する。 GetFullPath
パスからファイル名と拡張子を取得する。 GetFileName
パスからファイル名だけを取得する。 GetFileNameWithoutExtension
パスからディレクトリ名だけを取得する。 GetDirectoryName
ファイルの拡張子を変更する。 ChangeExtension
ファイルが存在するかどうかを判別する。 Exists

使用例

[Visual Basic, C#, C++] GetPathRoot メソッドを使用する例を次に示します。

 
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)

[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);

[C++] 
String * path = S"\\mydir\\";
String * fileName = S"myfile.ext";
String * fullPath = S"C:\\mydir\\myfile.ext";
String * pathRoot;

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

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

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

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Path クラス | Path メンバ | System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み