Path Path Class Fields Methods Remarks Applies to See also PathTooLongException Constructors Properties Methods Events Remarks Applies to See also Source/Reference
Provides a platform-specific alternate character used to separate directory levels in a path string that reflects a hierarchical file system organization.
Returns a value indicating whether the specified path string contains a root.
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, 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:".
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. Path members do, however, validate the contents of a specified path string, and throw an ArgumentException exception 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.
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.
Initializes a new instance of the PathTooLongException class with a specified error message and a reference to the inner exception that is the cause of this exception.
If your code does not have FileIOPermissionAttribute.PathDiscovery permission, the error message for this exception may contain only file or directory names instead of full paths.
In apps that run under versions of the .NET Framework prior to the .NET Framework 4.6.2, full paths must not exceed 260 characters to maintain compatibility with Windows operating systems. Any path in excess of 260 characters automatically throws a PathTooLongException. For more information about this restriction, see the entry Long Paths in .NET in the BCL Team blog.
Starting with apps running under the .NET Framework 4.6.2, the .NET Framework supports long paths in excess of 260 (or MAX_PATH) characters. The conditions under which a PathTooLongException exception are thrown depend on the version of the .NET Framework that an app targets:
Apps that target the .NET Framework 4.6.2 and later versions
Long paths are supported by default. The runtime throws a PathTooLongException under the following conditions:
The operating system returns COR_E_PATHTOOLONG or its equivalent.
The length of the path exceeds Int16.MaxValue (32,767) characters.
Apps that target the .NET Framework 4.6.1 and earlier versions
Long paths are disabled by default, and the legacy behavior is maintained. The runtime throws a PathTooLongException whenever a path exceeds 260 characters.
If this is undesirable, apps that target the .NET Framework 4.6.1 and earlier but run under the .NET Framework 4.6.2 can enable long path support by including the following setting in the <runtime> element of the application configuration file: