System.IO.Path Class
Provides methods for manipulating string paths. This class cannot be inherited.
Namespace
System.IO
Assembly
System.Runtime.dll
Syntax
public static class Path
Members
Properties
Name | Type | Description |
---|---|---|
DirectorySeparatorChar |
char | Gets the platform-specific directory separator character. |
AltDirectorySeparatorChar |
char | Gets an alternate directory separator character. |
PathSeparator |
char | Gets the platform-specific separator character used to separate path strings. |
VolumeSeparatorChar |
char | Gets the volume separator character (e.g., ':' on Windows). |
Methods
Name | Signature | Description |
---|---|---|
Combine |
static string Combine(params string[] paths) |
Combines an array of strings into a path. |
GetExtension |
static string GetExtension(string path) |
Returns the extension of the specified path string. |
GetFileName |
static string GetFileName(string path) |
Returns the file name and extension of the specified path string. |
GetFileNameWithoutExtension |
static string GetFileNameWithoutExtension(string path) |
Returns the file name of the specified path string without the extension. |
GetDirectoryName |
static string? GetDirectoryName(string path) |
Returns the directory information for the specified path string. |
GetFullPath |
static string GetFullPath(string path) |
Returns the absolute path for the specified path string. |
GetTempFileName |
static string GetTempFileName() |
Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file. |
GetTempPath |
static string GetTempPath() |
Returns the path of the current system's temporary folder. |
HasExtension |
static bool HasExtension(string path) |
Determines whether a path includes a file name extension. |
Example
using System;
using System.IO;
class Program
{
static void Main()
{
string[] parts = { "C:\\", "Users", "Alice", "Documents", "report.txt" };
string fullPath = Path.Combine(parts);
Console.WriteLine($"Combined: {fullPath}");
Console.WriteLine($"Extension: {Path.GetExtension(fullPath)}");
Console.WriteLine($"File name: {Path.GetFileName(fullPath)}");
Console.WriteLine($"Directory: {Path.GetDirectoryName(fullPath)}");
}
}
Remarks
The Path
class abstracts differences between operating systems, allowing developers to write cross‑platform file‑system code. Paths can be absolute or relative, and the class provides methods to safely combine and query them.