Table of Contents

Class Path

Namespace
System.IO
Assembly
System.IO.FileSystem.dll

Provides methods for processing file system strings in a cross-platform manner. Most of the methods don't do a complete parsing (such as examining a UNC hostname), but they will handle most string operations.

public static class Path
Inheritance
Path
Inherited Members

Fields

AltDirectorySeparatorChar

Provides a platform-specific alternate character used to separate directory levels in a path string that reflects a hierarchical file system organization.

public static readonly char AltDirectorySeparatorChar

Field Value

char

DirectorySeparatorChar

Provides a platform-specific character used to separate directory levels in a path string that reflects a hierarchical file system organization.

public static readonly char DirectorySeparatorChar

Field Value

char

PathSeparator

A platform-specific separator character used to separate path strings in environment variables.

public static readonly char PathSeparator

Field Value

char

VolumeSeparatorChar

Provides a platform-specific volume separator character.

public static readonly char VolumeSeparatorChar

Field Value

char

Methods

ChangeExtension(string, string)

Changes the extension of a path string.

public static string ChangeExtension(string path, string extension)

Parameters

path string

The path information to modify.

extension string

The new extension (with or without a leading period). Specify null to remove an existing extension from path.

Returns

string

The modified path information.

If path is null or an empty string (""), the path information is returned unmodified. If extension is null, the returned string contains the specified path with its extension removed. If path has no extension, and extension is not null, the returned path string contains extension appended to the end of path.

Combine(string, string)

Combines two strings into a path.

public static string Combine(string path1, string path2)

Parameters

path1 string

The first path to combine.

path2 string

The second path to combine.

Returns

string

The combined paths. If one of the specified paths is a zero-length string, this method returns the other path. If path2 contains an absolute path, this method returns path2.

Exceptions

ArgumentNullException

path1 or path2 is null.

GetDirectoryName(string)

Returns the directory portion of a file path. This method effectively removes the last segment 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 specified path is null, empty, or a root (such as "", "C:", or "\server\share").

public static string GetDirectoryName(string path)

Parameters

path string

Returns

string

Remarks

Directory separators are normalized in the returned string.

GetExtension(string)

Returns the extension (including the period ".") of the specified path string.

public static string GetExtension(string path)

Parameters

path string

The path string from which to get the extension.

Returns

string

The extension of the specified path (including the period "."), or null, or Empty. If path is null, GetExtension(string) returns null. If path does not have extension information, GetExtension(string) returns Empty.

GetFileName(string)

Returns the file name and extension of the specified path string.

public static string GetFileName(string path)

Parameters

path string

The path string from which to obtain the file name and extension.

Returns

string

The characters after the last directory separator character in path. If the last character of path is a directory or volume separator character, this method returns Empty. If path is null, this method returns null.

GetFileNameWithoutExtension(string)

Returns the file name of the specified path string without the extension.

public static string GetFileNameWithoutExtension(string path)

Parameters

path string

The path of the file.

Returns

string

The string returned by GetFileName(string), minus the last period (.) and all characters following it.

GetFullPath(string)

Returns the absolute path for the specified path string.

public static string GetFullPath(string path)

Parameters

path string

The file or directory for which to obtain absolute path information.

Returns

string

The fully qualified location of path, such as "C:\MyFile.txt".

GetInvalidFileNameChars()

Gets an array containing the characters that are not allowed in file names.

public static char[] GetInvalidFileNameChars()

Returns

char[]

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.

public static char[] GetInvalidPathChars()

Returns

char[]

An array containing the characters that are not allowed in path names.

GetPathRoot(string)

Gets the root directory information from the path contained in the specified string.

public static string GetPathRoot(string path)

Parameters

path string

A string containing the path from which to obtain root directory information.

Returns

string

The root directory of path if it is rooted.

-or-

Empty if path does not contain root directory information.

-or-

null if path is null or is effectively empty.

HasExtension(string)

Determines whether a path includes a file name extension.

public static bool HasExtension(string path)

Parameters

path string

The path to search for an extension.

Returns

bool

true if the characters that follow the last directory separator (\ or /) or volume separator (:) in the path include a period (.) followed by one or more characters; otherwise, false.

IsPathRooted(string)

Returns a value indicating whether the specified path string contains a root.

public static bool IsPathRooted(string path)

Parameters

path string

The path to test.

Returns

bool

true if path contains a root; otherwise, false.