Path Class

Namespace: System.IO

Assembly: System.Runtime.dll

Tip: This API is not available in the .NET Framework client profile. For more information, see .NET Framework Client Profile.

Syntax

public static class Path

The System.IO.Path class is a static class that provides methods for manipulating strings that represent file and directory paths. It handles common path operations such as combining paths, extracting file names, and getting directory names, while also accounting for platform-specific path syntax and conventions.

Methods

Name Description
Combine Combines a specified set of strings into a path.
GetDirectoryName Retrieves the parent directory's name from a path string.
GetExtension Retrieves the extension of the specified path string.
GetFileName Retrieves the file name and extension from a path string.
GetFileNameWithoutExtension Retrieves the file name without the extension from a path string.
GetFullPath Converts a relative path or fully qualified path into an absolute path.
GetInvalidFileNameChars Gets an enumerable collection of characters that are invalid in file or directory names.
GetInvalidPathChars Gets an enumerable collection of characters that are invalid in a path.
GetPathRoot Gets the root directory of a path string.
HasExtension Determines whether the specified path string contains file extension information.
IsPathRooted Determines whether the specified path string includes or is an absolute path.
Join Concatenates multiple strings into a path, separated by the current directory separator character.
RemoveExtension Removes the extension from the specified path string.

Method Details

Combine

public static string Combine (params string[] paths);

public static string Combine (string path1, string path2);

public static string Combine (string path1, string path2, string path3);

public static string Combine (string path1, string path2, string path3, string path4);

Combines multiple path segments into a single path, using the appropriate directory separator for the current operating system. This method intelligently handles cases where segments may already contain separators.

string fullPath = Path.Combine("C:\\Users", "Documents", "MyFile.txt");
// On Windows: "C:\Users\Documents\MyFile.txt"
// On Linux: "C:/Users/Documents/MyFile.txt" (or similar, depending on underlying OS details)

GetDirectoryName

public static string GetDirectoryName (string path);

Returns the directory information for the specified path string. This method returns null if the path is invalid or contains no directory information.

string path = @"C:\Users\Public\Documents\MyFile.txt";
string directory = Path.GetDirectoryName(path);
// directory will be @"C:\Users\Public\Documents"

GetExtension

public static string GetExtension (string path);

Returns the extension of the specified path string. The extension includes the leading period (e.g., ".txt"). If the path has no extension, it returns an empty string.

string path = "MyDocument.pdf";
string extension = Path.GetExtension(path);
// extension will be ".pdf"

GetFileName

public static string GetFileName (string path);

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

string path = @"C:\Program Files\MyApp\App.exe";
string fileName = Path.GetFileName(path);
// fileName will be "App.exe"

GetFileNameWithoutExtension

public static string GetFileNameWithoutExtension (string path);

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

string path = "MyImage.jpg";
string fileNameWithoutExt = Path.GetFileNameWithoutExtension(path);
// fileNameWithoutExt will be "MyImage"

GetFullPath

public static string GetFullPath (string path);

Converts a relative path or fully qualified path into an absolute path. If the path is already absolute, it is returned unchanged.

string relativePath = "MyFolder/MyFile.txt";
string absolutePath = Path.GetFullPath(relativePath);
// absolutePath will be the absolute path to MyFolder/MyFile.txt from the current working directory.

GetInvalidFileNameChars

public static char[] GetInvalidFileNameChars ();

Returns an array of characters that are not allowed in file or directory names on the current operating system.

GetInvalidPathChars

public static char[] GetInvalidPathChars ();

Returns an array of characters that are not allowed in any path component on the current operating system.

GetPathRoot

public static string GetPathRoot (string path);

Returns the root directory of the specified path string.

string path = @"\\Server\Share\Folder\File.txt";
string root = Path.GetPathRoot(path);
// root will be @"\\Server\Share\"

HasExtension

public static bool HasExtension (string path);

Determines whether the specified path string contains a file name extension.

bool hasExt1 = Path.HasExtension("MyFile.txt"); // true
bool hasExt2 = Path.HasExtension("MyFile");      // false

IsPathRooted

public static bool IsPathRooted (string path);

Determines whether the specified path string includes or is an absolute path. On Windows, this checks for drive letters (e.g., "C:\") or UNC paths (e.g., "\\server\share"). On Linux/macOS, it checks if the path starts with a forward slash.

bool isRooted1 = Path.IsPathRooted(@"C:\Temp\MyFile.txt"); // true (Windows)
bool isRooted2 = Path.IsPathRooted("MyFile.txt");         // false
bool isRooted3 = Path.IsPathRooted("/home/user/file.txt");  // true (Linux/macOS)

Join

public static string Join (params string[] paths);

public static string Join (string path1, string path2);

public static string Join (string path1, string path2, string path3);

public static string Join (string path1, string path2, string path3, string path4);

Concatenates multiple path segments, inserting the appropriate directory separator character between them. This method is an alternative to Combine and offers similar functionality.

string combinedPath = Path.Join("data", "images", "photo.jpg");
// On Windows: "data\images\photo.jpg"
// On Linux: "data/images/photo.jpg"

RemoveExtension

public static string RemoveExtension (string path);

Removes the extension from the specified path string, if one exists. Returns the original string if no extension is found.

string pathWithExt = "Report.docx";
string pathWithoutExt = Path.RemoveExtension(pathWithExt);
// pathWithoutExt will be "Report"

Note: The behavior of Path methods can vary slightly depending on the underlying operating system (Windows, Linux, macOS). For cross-platform compatibility, it's recommended to use these methods rather than manually constructing paths with hardcoded separators.