Directory.GetDirectoryRoot(String) メソッド

定義

指定したパスのボリューム情報、ルート情報、またはその両方を返します。

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

パラメーター

path
String

ファイルまたはディレクトリのパス。

返品

指定したパスのボリューム情報、ルート情報、またはその両方を含む文字列。

例外

呼び出し元に必要なアクセス許可がありません。

2.1 より前のバージョンの .NET Framework と .NET Core: path は長さ 0 の文字列で、空白のみを含むか、1 つ以上の無効な文字を含みます。 GetInvalidPathChars()を使用して無効な文字を照会できます。

pathnullです。

指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。

次の例は、現在のディレクトリを設定し、ディレクトリ ルートを表示する方法を示しています。

// This sample shows how to set the current directory and how to determine
// the root directory.
using System;
using System.IO;

namespace IOSamples
{
  public class DirectoryRoot
  {
    public static void Main()
    {
    // Create string for a directory. This value should be an existing directory
    // or the sample will throw a DirectoryNotFoundException.
      string dir = @"C:\test";		
      try
      {
          //Set the current directory.
          Directory.SetCurrentDirectory(dir);
      }
      catch (DirectoryNotFoundException e)
      {
          Console.WriteLine("The specified directory does not exist. {0}", e);
      }
    // Print to console the results.
      Console.WriteLine("Root directory: {0}", Directory.GetDirectoryRoot(dir));
      Console.WriteLine("Current directory: {0}", Directory.GetCurrentDirectory());
    }
  }
}
// The output of this sample depends on what value you assign to the variable dir.
// If the directory c:\test exists, the output for this sample is:
// Root directory: C:\
// Current directory: C:\test
// This sample shows how to set the current directory and how to determine
// the root directory.
open System.IO

// Create string for a directory. This value should be an existing directory
// or the sample will throw a DirectoryNotFoundException.
let dir = @"C:\test"
try
    //Set the current directory.
    Directory.SetCurrentDirectory dir
with :? DirectoryNotFoundException as e ->
    printfn $"The specified directory does not exist. {e}"
    
// Print to console the results.
printfn $"Root directory: {Directory.GetDirectoryRoot dir}"
printfn $"Current directory: {Directory.GetCurrentDirectory()}"
// The output of this sample depends on what value you assign to the variable dir.
// If the directory c:\test exists, the output for this sample is:
// Root directory: C:\
// Current directory: C:\test
' This sample shows how to set the current directory and how to determine
' the root directory.
Imports System.IO

Public Class DirectoryRoot
   
   Public Shared Sub Main()
      ' Create string for a directory. This value should be an existing directory
      ' or the sample will throw a DirectoryNotFoundException.
      Dim dir As String = "C:\test"
      Try
         'Set the current directory.
         Directory.SetCurrentDirectory(dir)
      Catch e As DirectoryNotFoundException
         Console.WriteLine("The specified directory does not exist. {0}", e)
      End Try
      ' Print to console the results.
      Console.WriteLine("Root directory: {0}", Directory.GetDirectoryRoot(dir))
      Console.WriteLine("Current directory: {0}", Directory.GetCurrentDirectory())
   End Sub
End Class
' The output of this sample depends on what value you assign to the variable dir.
' If the directory c:\test exists, the output for this sample is:
' Root directory: C:\
' Current directory: C:\test

注釈

このメソッドは、GetFullPathによって返されるpathの完全修飾パス名を取得し、ルート ディレクトリ情報を返します。 指定したパスは存在する必要はありません。

path パラメーターは、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、 GetCurrentDirectoryを参照してください。

path パラメーターの大文字と小文字の区別は、コードが実行されているファイル システムの大文字と小文字が区別されます。 たとえば、NTFS (既定の Windows ファイル システム) では大文字と小文字が区別され、Linux ファイル システムでは大文字と小文字が区別されます。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

適用対象

こちらもご覧ください