Class Path
Performs operations on String instances that contain file or directory path information.
Inherited Members
Namespace: System.IO
Assembly: System.IO.FileSystem.dll
Syntax
public sealed class Path
Fields
DirectorySeparatorChar
Provides a platform-specific character used to separate directory levels in a path string that reflects a hierarchical file system organization.
Declaration
public static readonly char DirectorySeparatorChar
Field Value
Type | Description |
---|---|
char |
InvalidPathChars
Provides a platform-specific array of characters that cannot be specified in path string arguments passed to members of the Path class.
Declaration
public static readonly char[] InvalidPathChars
Field Value
Type | Description |
---|---|
char[] |
Methods
ChangeExtension(string, string)
Changes the extension of a file path. The
path
parameter
specifies a file path, and the extension
parameter
specifies a file extension (with a leading period, such as
".exe"
or".cool"
).
The function returns a file path with the same root, directory, and base name parts as
path
, but with the file extension changed to
the specified extension.Ifpath
is null, the function
returns null. If path
does not contain a file extension,
the new file extension is appended to the path.Ifextension
is null, any existing extension is removed from path
.
Declaration
public static string ChangeExtension(string path, string extension)
Parameters
Type | Name | Description |
---|---|---|
string | path | The path for which to change file extension. |
string | extension | The new file extension (with a leading period), or null to remove the extension. |
Returns
Type | Description |
---|---|
string |
Combine(string, string)
Combines two strings into a path.
Declaration
public static string Combine(string path1, string path2)
Parameters
Type | Name | Description |
---|---|---|
string | path1 | The first path to combine. |
string | path2 | The second path to combine. |
Returns
Type | Description |
---|---|
string |
GetDirectoryName(string)
Returns the directory path of a file path. This method effectively removes the last element of the given file path, i.e.it returns a string consisting of all characters up to but not including the last backslash("") in the file path. The returned value is null if the file path is null or if the file path denotes a root (such as "", "C:", or "\server\share").
Declaration
public static string GetDirectoryName(string path)
Parameters
Type | Name | Description |
---|---|---|
string | path | The path of a file or directory. |
Returns
Type | Description |
---|---|
string | The directory path of the given path, or null if the given path denotes a root. |
GetExtension(string)
Returns the extension of the given path. The returned value includes the period(".") character of the extension except when you have a terminal period when you get String.Empty, such as
".exe"
or
".cpp"
. The returned value is null if the given path is
null or if the given path does not include an extension.
Declaration
public static string GetExtension(string path)
Parameters
Type | Name | Description |
---|---|---|
string | path | The path of a file or directory. |
Returns
Type | Description |
---|---|
string | The extension of the given path, or null if the given path does not include an extension. |
Exceptions
Type | Condition |
---|---|
ArgumentException | if path contains invalid characters. |
GetFileName(string)
Returns the name and extension parts of the given path. The resulting string contains the characters of
path
that follow the last
backslash (""), slash ("/"), or colon (":") character in
path
.The resulting string is the entire path if path
contains no backslash after removing trailing slashes, slash, or colon characters.The resulting string is null if
path
is null.
Declaration
public static string GetFileName(string path)
Parameters
Type | Name | Description |
---|---|---|
string | path | The path of a file or directory. |
Returns
Type | Description |
---|---|
string | The name and extension parts of the given path. |
Exceptions
Type | Condition |
---|---|
ArgumentException | if path contains invalid characters. |
GetFileNameWithoutExtension(string)
Returns the file name of the specified path string without the extension.
Declaration
public static string GetFileNameWithoutExtension(string path)
Parameters
Type | Name | Description |
---|---|---|
string | path | The path of the file. |
Returns
Type | Description |
---|---|
string |
GetFullPath(string)
Returns the absolute path for the specified path string.
Declaration
public static string GetFullPath(string path)
Parameters
Type | Name | Description |
---|---|---|
string | path | The file or directory for which to obtain absolute path information. |
Returns
Type | Description |
---|---|
string |
GetInvalidPathChars()
Gets an array containing the characters that are not allowed in path names.
Declaration
public static char[] GetInvalidPathChars()
Returns
Type | Description |
---|---|
char[] | An array containing the characters that are not allowed in path names. |
GetPathRoot(string)
Tests if a path includes a file extension. The result is
true
if the characters that follow the last directory
separator('\' or '/') or volume separator(':') in the path include a period(".") other than a terminal period.The result is
false
otherwise.
Declaration
public static string GetPathRoot(string path)
Parameters
Type | Name | Description |
---|---|---|
string | path | The path of a file or directory. |
Returns
Type | Description |
---|---|
string | The root portion of the given path. |
Exceptions
Type | Condition |
---|---|
ArgumentException | if path contains invalid characters. |
HasExtension(string)
Tests if a path includes a file extension. The result is
true
if the characters that follow the last directory
separator('\' or '/') or volume separator(':') in the path include a period(".") other than a terminal period.The result is
false
otherwise.
Declaration
public static bool HasExtension(string path)
Parameters
Type | Name | Description |
---|---|---|
string | path | The path to test. |
Returns
Type | Description |
---|---|
bool | Boolean indicating whether the path includes a file extension. |
Exceptions
Type | Condition |
---|---|
ArgumentException | if path contains invalid characters. |
IsPathRooted(string)
Tests if the given path contains a root. A path is considered rooted if it starts with a backslash("") or a drive letter and a colon (":").
Declaration
public static bool IsPathRooted(string path)
Parameters
Type | Name | Description |
---|---|---|
string | path | The path to test. |
Returns
Type | Description |
---|---|
bool | Boolean indicating whether the path is rooted. |
Exceptions
Type | Condition |
---|---|
ArgumentException | if path contains invalid characters. |