Class Path
Represents a collection of static methods for creating, manipulating, and querying path and file names.
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Inheritance: Object > Path
Summary
The Path
class provides methods for working with strings that represent file and directory paths. These methods are useful for parsing path strings, combining them, and retrieving parts of them. The Path
class is designed to handle platform-specific path formats automatically.
Methods
Name | Description |
---|---|
string Combine (string path1, string path2)
|
Combines two strings into a path. |
string Combine (string path1, string path2, string path3)
|
Combines three strings into a path. |
string GetDirectoryName (string path)
|
Retrieves the parent directory's name of the specified path string. |
string GetExtension (string path)
|
Retrieves the extension of the specified path string. |
string GetFileName (string path)
|
Retrieves the file name/extension of the specified path string. |
string GetFileNameWithoutExtension (string path)
|
Retrieves the file name without the extension of the specified path string. |
string GetFullPath (string path)
|
Gets the combined absolute path of the specified path string. |
string GetPathRoot (string path)
|
Gets the root directory portion of the specified path string. |
bool HasExtension (string path)
|
Determines whether the specified path string includes an extension. |
Remarks
The Path
class provides a set of static methods that are useful for common path manipulation operations. These methods are designed to be cross-platform compatible, meaning they will work correctly on different operating systems (like Windows, macOS, and Linux) by using the appropriate path separators and conventions for each platform.
When combining paths, the Path.Combine
method intelligently handles the directory separators. For example, if the first path ends with a separator, it won't add another one. If the second path starts with a separator, it will also be handled correctly.
It's important to note that the Path
methods do not perform validation on the input path strings beyond basic structural checks. They do not check if the path actually exists on the file system or if it's a valid file or directory name according to the operating system's rules.
Example Usage
// Example of using Path.Combine
string directory = @"C:\Users\Public";
string file = "document.txt";
string fullPath = Path.Combine(directory, file);
Console.WriteLine(fullPath);
// Output: C:\Users\Public\document.txt
// Example of getting the directory name
string filePath = @"C:\MyDocuments\Reports\report.docx";
string dirName = Path.GetDirectoryName(filePath);
Console.WriteLine(dirName);
// Output: C:\MyDocuments\Reports
// Example of getting the file extension
string fileName = "image.jpeg";
string extension = Path.GetExtension(fileName);
Console.WriteLine(extension);
// Output: .jpeg
// Example of checking if a path has an extension
bool hasExt = Path.HasExtension("myconfig");
Console.WriteLine(hasExt);
// Output: False
hasExt = Path.HasExtension("myconfig.json");
Console.WriteLine(hasExt);
// Output: True