Path Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Performs operations on String instances that contain file or directory path information. These operations are performed in a cross-platform manner.
public ref class Path abstract sealed
public ref class Path sealed
public static class Path
public sealed class Path
[System.Runtime.InteropServices.ComVisible(true)]
public static class Path
type Path = class
[<System.Runtime.InteropServices.ComVisible(true)>]
type Path = class
Public Class Path
Public NotInheritable Class Path
- Inheritance
-
Path
- Attributes
Examples
The following example demonstrates some of the main members of the Path
class.
using namespace System;
using namespace System::IO;
int main()
{
String^ path1 = "c:\\temp\\MyTest.txt";
String^ path2 = "c:\\temp\\MyTest";
String^ path3 = "temp";
if ( Path::HasExtension( path1 ) )
{
Console::WriteLine( "{0} has an extension.", path1 );
}
if ( !Path::HasExtension( path2 ) )
{
Console::WriteLine( "{0} has no extension.", path2 );
}
if ( !Path::IsPathRooted( path3 ) )
{
Console::WriteLine( "The string {0} contains no root information.", path3 );
}
Console::WriteLine( "The full path of {0} is {1}.", path3, Path::GetFullPath( path3 ) );
Console::WriteLine( "{0} is the location for temporary files.", Path::GetTempPath() );
Console::WriteLine( "{0} is a file available for use.", Path::GetTempFileName() );
Console::WriteLine( "\r\nThe set of invalid characters in a path is:" );
Console::WriteLine( "(Note that the wildcard characters '*' and '?' are not invalid.):" );
Collections::IEnumerator^ myEnum = Path::InvalidPathChars->GetEnumerator();
while ( myEnum->MoveNext() )
{
Char c = *safe_cast<Char^>(myEnum->Current);
Console::WriteLine( c );
}
}
using System;
using System.IO;
class Test
{
public static void Main()
{
string path1 = @"c:\temp\MyTest.txt";
string path2 = @"c:\temp\MyTest";
string path3 = @"temp";
if (Path.HasExtension(path1))
{
Console.WriteLine("{0} has an extension.", path1);
}
if (!Path.HasExtension(path2))
{
Console.WriteLine("{0} has no extension.", path2);
}
if (!Path.IsPathRooted(path3))
{
Console.WriteLine("The string {0} contains no root information.", path3);
}
Console.WriteLine("The full path of {0} is {1}.", path3, Path.GetFullPath(path3));
Console.WriteLine("{0} is the location for temporary files.", Path.GetTempPath());
Console.WriteLine("{0} is a file available for use.", Path.GetTempFileName());
/* This code produces output similar to the following:
* c:\temp\MyTest.txt has an extension.
* c:\temp\MyTest has no extension.
* The string temp contains no root information.
* The full path of temp is D:\Documents and Settings\cliffc\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\temp.
* D:\Documents and Settings\cliffc\Local Settings\Temp\8\ is the location for temporary files.
* D:\Documents and Settings\cliffc\Local Settings\Temp\8\tmp3D.tmp is a file available for use.
*/
}
}
Imports System.IO
Public Class Test
Public Shared Sub Main()
Dim path1 As String = "c:\temp\MyTest.txt"
Dim path2 As String = "c:\temp\MyTest"
Dim path3 As String = "temp"
If Path.HasExtension(path1) Then
Console.WriteLine("{0} has an extension.", path1)
End If
If Path.HasExtension(path2) = False Then
Console.WriteLine("{0} has no extension.", path2)
End If
If Path.IsPathRooted(path3) = False Then
Console.WriteLine("The string {0} contains no root information.", path3)
End If
Console.WriteLine("The full path of {0} is {1}.", path3, Path.GetFullPath(path3))
Console.WriteLine("{0} is the location for temporary files.", Path.GetTempPath())
Console.WriteLine("{0} is a file available for use.", Path.GetTempFileName())
' This code produces output similar to the following:
' c:\temp\MyTest.txt has an extension.
' c:\temp\MyTest has no extension.
' The string temp contains no root information.
' The full path of temp is D:\Documents and Settings\cliffc\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\temp.
' D:\Documents and Settings\cliffc\Local Settings\Temp\8\ is the location for temporary files.
' D:\Documents and Settings\cliffc\Local Settings\Temp\8\tmp3D.tmp is a file available for use.
End Sub
End Class
Remarks
A path is a string that provides the location of a file or directory. A path does not necessarily point to a location on disk; for example, a path might map to a location in memory or on a device. The exact format of a path is determined by the current platform. For example, on some systems, a path can start with a drive or volume letter, while this element is not present in other systems. On some systems, file paths can contain extensions, which indicate the type of information stored in the file. The format of a file name extension is platform-dependent; for example, some systems limit extensions to three characters (such as FAT16 commonly used on smaller flash storage and older versions of ISO 9660 used on optical media), and others do not. The current platform also determines the set of characters used to separate the elements of a path, and the set of characters that cannot be used when specifying paths. Because of these differences, the fields of the Path
class as well as the exact behavior of some members of the Path
class are platform-dependent.
A path can contain absolute or relative location information. Absolute paths fully specify a location: the file or directory can be uniquely identified regardless of the current location. Relative paths specify a partial location: the current location is used as the starting point when locating a file specified with a relative path. To determine the current directory, call Directory.GetCurrentDirectory.
.NET Core 1.1 and later versions and .NET Framework 4.6.2 and later versions also support access to file system objects that are device names, such as "\\?\C:\".
For more information on file path formats on Windows, see File path formats on Windows systems.
Most members of the Path
class do not interact with the file system and do not verify the existence of the file specified by a path string. Path
class members that modify a path string, such as ChangeExtension, have no effect on names of files in the file system.
Some Path
members do validate the contents of a specified path string, and throw an ArgumentException if the string contains characters that are not valid in path strings, as defined in the characters returned from the GetInvalidPathChars method. For example, on Windows-based desktop platforms, invalid path characters might include quote ("), less than (<), greater than (>), pipe (|), backspace (\b), null (\0), and Unicode characters 16 through 18 and 20 through 25. This validation behavior varies between .NET versions:
On .NET Framework and .NET Core versions older than 2.1: All
Path
members that take a path as an argument throw an ArgumentException if they detect invalid path characters.On .NET Core 2.1 and later versions: GetFullPath is the only member that throws an ArgumentException if the string contains invalid path characters.
The members of the Path
class enable you to quickly and easily perform common operations such as determining whether a file name extension is part of a path, and combining two strings into one path name.
All members of the Path
class are static and can therefore be called without having an instance of a path.
Note
In members that accept a path as an input string, that path must be well-formed or an exception is raised. For example, if a path is fully qualified but begins with a space, the path is not trimmed in methods of the class. Therefore, the path is malformed and an exception is raised. Similarly, a path or a combination of paths cannot be fully qualified twice. For example, "c:\temp c:\windows" also raises an exception in most cases. Ensure that your paths are well-formed when using methods that accept a path string.
In members that accept a path, the path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. For example, all the following are acceptable paths:
"c:\\MyDir\\MyFile.txt" in C#, or "c:\MyDir\MyFile.txt" in Visual Basic.
"c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.
"MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.
"\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.
Because all these operations are performed on strings, it is impossible to verify that the results are valid in all scenarios. For example, the GetExtension method parses a string that you pass to it and returns the extension from that string. However, this does not mean that a file with that extension exists on the disk.
For a list of common I/O tasks, see Common I/O Tasks.
Fields
AltDirectorySeparatorChar |
Provides a platform-specific alternate character used to separate directory levels in a path string that reflects a hierarchical file system organization. |
DirectorySeparatorChar |
Provides a platform-specific character used to separate directory levels in a path string that reflects a hierarchical file system organization. |
InvalidPathChars |
Obsolete.
Obsolete.
Provides a platform-specific array of characters that cannot be specified in path string arguments passed to members of the Path class. |
PathSeparator |
A platform-specific separator character used to separate path strings in environment variables. |
VolumeSeparatorChar |
Provides a platform-specific volume separator character. |
Methods
ChangeExtension(String, String) |
Changes the extension of a path string. |
Combine(ReadOnlySpan<String>) |
Combines a span of strings into a path. |
Combine(String, String) |
Combines two strings into a path. |
Combine(String, String, String) |
Combines three strings into a path. |
Combine(String, String, String, String) |
Combines four strings into a path. |
Combine(String[]) |
Combines an array of strings into a path. |
EndsInDirectorySeparator(ReadOnlySpan<Char>) |
Returns a value that indicates whether the path, specified as a read-only span, ends in a directory separator. |
EndsInDirectorySeparator(String) |
Returns a value that indicates whether the specified path ends in a directory separator. |
Exists(String) |
Determines whether the specified file or directory exists. |
GetDirectoryName(ReadOnlySpan<Char>) |
Returns the directory information for the specified path represented by a character span. |
GetDirectoryName(String) |
Returns the directory information for the specified path. |
GetExtension(ReadOnlySpan<Char>) |
Returns the extension of a file path that is represented by a read-only character span. |
GetExtension(String) |
Returns the extension (including the period ".") of the specified path string. |
GetFileName(ReadOnlySpan<Char>) |
Returns the file name and extension of a file path that is represented by a read-only character span. |
GetFileName(String) |
Returns the file name and extension of the specified path string. |
GetFileNameWithoutExtension(ReadOnlySpan<Char>) |
Returns the file name without the extension of a file path that is represented by a read-only character span. |
GetFileNameWithoutExtension(String) |
Returns the file name of the specified path string without the extension. |
GetFullPath(String) |
Returns the absolute path for the specified path string. |
GetFullPath(String, String) |
Returns an absolute path from a relative path and a fully qualified base path. |
GetInvalidFileNameChars() |
Gets an array containing the characters that are not allowed in file names. |
GetInvalidPathChars() |
Gets an array containing the characters that are not allowed in path names. |
GetPathRoot(ReadOnlySpan<Char>) |
Gets the root directory information from the path contained in the specified character span. |
GetPathRoot(String) |
Gets the root directory information from the path contained in the specified string. |
GetRandomFileName() |
Returns a random folder name or file name. |
GetRelativePath(String, String) |
Returns a relative path from one path to another. |
GetTempFileName() |
Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file. |
GetTempPath() |
Returns the path of the current user's temporary folder. |
HasExtension(ReadOnlySpan<Char>) |
Determines whether the path represented by the specified character span includes a file name extension. |
HasExtension(String) |
Determines whether a path includes a file name extension. |
IsPathFullyQualified(ReadOnlySpan<Char>) |
Returns a value that indicates whether the file path represented by the specified character span is fixed to a specific drive or UNC path. |
IsPathFullyQualified(String) |
Returns a value that indicates whether the specified file path is fixed to a specific drive or UNC path. |
IsPathRooted(ReadOnlySpan<Char>) |
Returns a value that indicates whether the specified character span that represents a file path contains a root. |
IsPathRooted(String) |
Returns a value indicating whether the specified path string contains a root. |
Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>) |
Concatenates two path components into a single path. |
Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>) |
Concatenates three path components into a single path. |
Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>) |
Concatenates four path components into a single path. |
Join(ReadOnlySpan<String>) |
Concatenates a span of paths into a single path. |
Join(String, String) |
Concatenates two paths into a single path. |
Join(String, String, String) |
Concatenates three paths into a single path. |
Join(String, String, String, String) |
Concatenates four paths into a single path. |
Join(String[]) |
Concatenates an array of paths into a single path. |
TrimEndingDirectorySeparator(ReadOnlySpan<Char>) |
Trims one trailing directory separator beyond the root of the specified path. |
TrimEndingDirectorySeparator(String) |
Trims one trailing directory separator beyond the root of the specified path. |
TryJoin(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, Span<Char>, Int32) |
Attempts to concatenate three path components to a single preallocated character span, and returns a value that indicates whether the operation succeeded. |
TryJoin(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Span<Char>, Int32) |
Attempts to concatenate two path components to a single preallocated character span, and returns a value that indicates whether the operation succeeded. |
Applies to
See also
Povratne informacije
https://aka.ms/ContentUserFeedback.
Kmalu na voljo: V letu 2024 bomo ukinili storitev Težave v storitvi GitHub kot mehanizem za povratne informacije za vsebino in jo zamenjali z novim sistemom za povratne informacije. Za več informacij si oglejte:Pošlji in prikaži povratne informacije za