Mono Class Library: System.IO Namespace

System.IO.Path Class

Performs operations on string instances that contain file or directory path information. [Edit]

See Also: Path Members

[System.Runtime.InteropServices.ComVisible(true)]
public static class Path

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

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. Paths are composed of the components described below. Component names are shown in italics and the following table describes the symbols used in component definitions:

SymbolDescription
< >Indicates a path component.
{ } Indicates a grouping; either all components in a grouping are present, or none are permitted to be present.
*Indicates that the component or grouping that immediately precedes this symbol can appear zero, one, or multiple times.
?Indicates that the component or grouping that immediately precedes this symbol can appear zero, or one times.
+Indicates string concatenation.

The components that define a path are as follows:

Directory Name: A string that specifies one or more directory levels in a file system. If a directory name contains multiple levels, a directory separator character separates the levels; however, a directory name does not begin or end with a directory separator character. In the example path C:/foo/bar/bat.txt, the directory name is "foo/bar". Path.GetDirectoryName(string) returns the directory name component of a path. Note that this method does include a beginning separator character if one is included in the specified path.

Directory Separator Character: An implementation-specific constant string containing a single printable non-alphanumeric character used to separate levels in a file system. In the example path C:/foo/bar/bat.txt, the directory separator character is "/". The Path.DirectorySeparatorChar and Path.AltDirectorySeparatorChar store implementation-specific directory separator characters .

Extension: A string that consists of the characters at the end of a file name, from and including the last extension separator character. The minimum and maximum lengths of extension components are implementation-specific. In the example path C:/foo/bar/bat.txt , the extension is ".txt". The Path.GetExtension(string) method returns the extension component of a path.

Extension Separator Character: An implementation-specific constant string composed of a single character that appears after the last character in the file base component indicating the beginning of the extension component. If the extension separator character is the first character in a file name, it is not interpreted as a extension separator character. If more than one extension separator character appears in a file name, only the last occurrence is the extension separator character; all other occurrences are part of the file base component. In the example path C:/foo/bar/bat.txt, the extension separator character is ".".

File Base: A string containing the filename with the extension component removed. In the example path C:/foo/bar/bat.txt, the file base is "bat". The Path.GetFileNameWithoutExtension(string) method returns the file base component of a path.

File Name: A string containing all information required to uniquely identify a file within a directory. This component is defined as follows:

<file base>{+<extension>}?

The file name component is commonly referred to as a relative file name. In the example path C:/foo/bar/bat.txt, the file name is "bat.txt". The Path.GetFileName(string) method returns the file name component of a path.

Full Directory Name: A string containing all information required to uniquely identify a directory within a file system. This component is defined as follows:

<path root>+<directory name>

The full directory name component is commonly referred to as the absolute directory name. In the example path C:/foo/bar/bat.txt, the full directory name is "C:/foo/bar ".

FullPath: A string containing all information required to uniquely identify a file within a file system. This component is defined as follows:

<full directory name>+<directory separator character>+<file name>

The full path component is commonly referred to as the absolute file name. In the example path C:/foo/bar/bat.txt, the full path is "C:/foo/bar/bat.txt". The Path.GetFullPath(string) method returns the full path component.

Path Root: A string containing all information required to uniquely identify the highest level in a file system. The component is defined as follows:

{<volume identifier>+<volume separator character>}?+<directory separator character>

In the example path C:/foo/bar/bat.txt , the path root is "C:/". The Path.GetPathRoot(string) method returns the path root component.

VolumeIdentifier: A string composed of a single alphabetic character that uniquely defines a drive or volume in a file system. This component is optional; on systems that do not support volume identifiers, this component is required to be a zero length string. In the example path C:/foo/bar/bat.txt , the path root is "C:". In the example path, \\myserver\myshare\foo\bar\baz.txt the path root is "\\myserver\myshare".

Volume Separator Character: A string composed of a single alphabetic character used to separate the volumeidentifier from other components in a path. This component can appear in a path only if a volume identifier is present. This component is optional; on systems that do not support the volume identifier component, the volume separator character component is required to be a zero length string.

The exact format of a path is determined by the current platform. For example, on Windows systems a path can start with a volume identifier, while this element is not present in Unix system paths. On some systems, paths containing file names can contain extensions. The format of an extension is platform dependent; for example, some systems limit extensions to three characters, while others do not. The current platform and possibly the current file system determine 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 System.IO.Path class as well as the exact behavior of some members of the System.IO.Path class are determined by the current platform and/or file system.

A path contains either absolute or relative location information. Absolute paths fully specify a location: the file or directory can be uniquely identified regardless of the current location. A full path or full directory name component is present in an absolute path. Relative paths specify a partial location: the current working directory is used as the starting point when locating a file specified with a relative path.

Note: To determine the current working directory, call Directory.GetCurrentDirectory .

Most members of the Path class do not interact with the file system and do not verify the existence of the file or directory specified by a path string. System.IO.Path members that modify a path string, such as Path.ChangeExtension(string, string), have no effect on files and directories in the file system. System.IO.Path members do, however, validate the contents of a specified path string, and throw ArgumentException if the string contains characters that are not valid in path strings, as defined by the current platform and file system. Implementations are required to preserve the case of file and directory path strings, and to be case sensitive if and only if the current platform is case-sensitive.

[Edit]

Requirements

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0