Path.GetPathRoot Metodo

Definizione

Overload

GetPathRoot(String)

Ottiene le informazioni sulla directory radice dal percorso contenuto nella stringa specificata.

GetPathRoot(ReadOnlySpan<Char>)

Ottiene la directory radice dal percorso contenuto nell'intervallo di caratteri specificato.

GetPathRoot(String)

Ottiene le informazioni sulla directory radice dal percorso contenuto nella stringa specificata.

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

Parametri

path
String

Stringa contenente il percorso dal quale ottenere informazioni sulla directory radice.

Restituisce

Directory radice di path se contiene la radice.

-oppure-

Empty se path non contiene informazioni sulla directory radice.

-oppure-

null se path è null o è effettivamente vuoto.

Eccezioni

.NET Framework e versioni di .NET Core precedenti alla 2.1: path contiene uno o più caratteri non validi definiti in GetInvalidPathChars().

-oppure-

Solo .NET Framework: Empty è stato passato a path.

Esempio

Nell'esempio seguente viene illustrato l'uso del GetPathRoot metodo .

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:\'

Commenti

Questo metodo non verifica che il percorso o il file esista.

Questo metodo normalizzerà i separatori di directory.

Una stringa è "effettivamente vuota" se:

  • In Windows la chiamata IsEmpty a questa stringa restituisce trueo tutti i relativi caratteri sono spazi (' ').
  • In Unix chiamare IsNullOrEmpty su questa stringa restituisce true.

I modelli possibili per la stringa restituita da questo metodo sono i seguenti:

  • null (path è null o una stringa vuota).

  • Stringa vuota (path specificato un percorso relativo nell'unità o nel volume corrente).

  • "/" (Unix: path specificato un percorso assoluto nell'unità corrente).

  • "X:" (Windows: path specificato un percorso relativo in un'unità, dove X rappresenta un'unità o una lettera di volume).

  • "X:\" (Windows: path specificato un percorso assoluto in una determinata unità).

  • "\\ComputerName\SharedFolder" (Windows: un percorso UNC).

  • "\?\C:" (Windows: percorso del dispositivo DOS, supportato in .NET Core 1.1 e versioni successive e in .NET Framework 4.6.2 e versioni successive).

Per altre informazioni sui percorsi di file in Windows, vedere Formati di percorso dei file nei sistemi Windows. Per un elenco delle attività di I/O comuni, vedere Attività di I/O comuni.

Vedi anche

Si applica a

GetPathRoot(ReadOnlySpan<Char>)

Ottiene la directory radice dal percorso contenuto nell'intervallo di caratteri specificato.

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)

Parametri

path
ReadOnlySpan<Char>

Intervallo di sola lettura di caratteri contenente il percorso dal quale ottenere informazioni sulla directory radice.

Restituisce

Intervallo di sola lettura di caratteri contenente la directory radice di path.

Commenti

Questo metodo non verifica che il percorso o il file esista.

A differenza dell'overload di stringhe, questo metodo non normalizza i separatori di directory.

Un ReadOnlySpan<System.Char> oggetto è "effettivamente vuoto" se:

  • In Windows la chiamata ReadOnlySpan<T>.IsEmpty a questo intervallo di caratteri restituisce trueo tutti i relativi caratteri sono spazi (' ').
  • In Unix la chiamata ReadOnlySpan<T>.IsEmpty a questo intervallo di caratteri restituisce true.

I modelli possibili per l'intervallo di caratteri di sola lettura restituiti da questo metodo sono i seguenti:

  • ReadOnlySpan<T>.Empty (path era ReadOnlySpan<T>.Empty.

  • ReadOnlySpan<T>.Empty (path specificato un percorso relativo nell'unità o nel volume corrente).

  • "/" (Unix: path specificato un percorso assoluto nell'unità corrente).

  • "X:" (Windows: path specificato un percorso relativo in un'unità, dove X rappresenta un'unità o una lettera di volume).

  • "X:\" (Windows: path specificato un percorso assoluto in una determinata unità).

  • "\\ComputerName\SharedFolder" (Windows: un percorso UNC).

  • "\?\C:" (Windows: percorso del dispositivo DOS, supportato in .NET Core 1.1 e versioni successive e in .NET Framework 4.6.2 e versioni successive).

Per altre informazioni sui percorsi di file in Windows, vedere Formati di percorso dei file nei sistemi Windows. Per un elenco delle attività di I/O comuni, vedere Attività di I/O comuni.

Vedi anche

Si applica a