System.IO.Path Class

Summary

Provides methods for creating, manipulating, and processing instances of Path, which represent file or directory paths.

Remarks

The Path class is a static class, meaning you do not create an instance of it. All members of the Path class are static members.

It is important to note that this class handles path data in a manner that is compatible with the underlying file system, but it does not interact with the file system itself. For example, Path.GetFileName does not return null for a non-existent file name.

To work with file system entries, use classes such as File, Directory, and FileInfo.

Methods

Path.Combine(string path1, string path2)

Summary

Concatenates two paths and returns the combined path.

Remarks

This method is overloaded to accept any number of path segments.

Syntax

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

Parameters

Name Type Description
path1 string The first path to combine.
path2 string The second path to combine.

Return Value

The combined path.

Example

string directory = @"C:\Documents";
string fileName = "report.docx";
string fullPath = Path.Combine(directory, fileName);
// fullPath will be @"C:\Documents\report.docx"

Path.GetDirectoryName(string path)

Summary

Retrieves the directory name of the specified path string.

Remarks

This method returns null if path is null, or an empty string if path does not contain directory information.

Syntax

public static string GetDirectoryName(string path);

Parameters

Name Type Description
path string The path from which to retrieve the directory name.

Return Value

The directory name of path, or null if path is null or empty.

Path.GetFileName(string path)

Summary

Retrieves the file name of the specified path string.

Remarks

This method returns null if path is null, or an empty string if path ends with a directory separator character.

Syntax

public static string GetFileName(string path);

Parameters

Name Type Description
path string The path from which to retrieve the file name.

Return Value

The characters after the last directory separator character in path. Returns null if path is null or empty.

Inherited Members

  • Object.Equals
  • Object.Finalize
  • Object.GetType
  • Object.MemberwiseClone
  • Object.ReferenceEquals
  • Object.ToString