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

파일 또는 디렉터리의 경로입니다.

반환

String

지정된 경로에 대한 볼륨 정보, 루트 정보 또는 두 정보 모두를 포함하는 문자열입니다.

예외

호출자에게 필요한 권한이 없는 경우

2.1보다 오래된 .NET Framework 및 .NET Core 버전: path 길이가 0인 문자열이거나, 공백만 포함하거나, 하나 이상의 잘못된 문자를 포함합니다. GetInvalidPathChars()를 사용하여 잘못된 문자를 쿼리할 수 있습니다.

path이(가) null인 경우

지정된 경로, 파일 이름 또는 둘 다가 시스템에서 정의한 최대 길이를 초과합니다.

예제

다음 예제에서는 현재 디렉터리를 설정하고 디렉터리 루트를 표시하는 방법을 보여 줍니다.

// This sample shows how to set the current directory and how to determine
// the root directory.
using namespace System;
using namespace System::IO;
int 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.
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 대/소문자를 구분하지 않습니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

적용 대상

추가 정보