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

文件或目录的路径。

返回

包含指定路径的卷信息、根信息或同时包括这两者的字符串。

例外

调用方没有所要求的权限。

.NET Framework 和 .NET Core 版本早于 2.1: path 是零长度字符串,仅包含空格,或包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 查询无效字符。

pathnull

指定的路径和/或文件名超过了系统定义的最大长度。

示例

下面的示例演示如何设置当前目录并显示目录根目录。

// 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

注解

此方法获取 返回GetFullPathpath的完全限定路径名,并返回根目录信息。 指定的路径不需要存在。

允许 path 参数指定相对路径信息或绝对路径信息。 相对路径信息被解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory

参数的 path 区分大小写对应于运行代码的文件系统的区分大小写。 例如,它在 NTFS 上不区分大小写, (默认 Windows 文件系统) ,在 Linux 文件系统上区分大小写。

有关常见 I/O 任务的列表,请参阅 常见 I/O 任务

适用于

另请参阅