Path Class

Overview

  • Provides helper methods for working with file system paths as strings.

Examples

Change Extensions

// This does NOT rename the file! It is only a string operation.
Console.WriteLine(Path.ChangeExtension(@"C:\Public\Test.txt", ".doc"));

Combine Paths

string root = @"C:\Public\Test";
string subdir = @"Subtest\";
string combined = Path.Combine(root, subdir);
Console.WriteLine(combined);
 
// If there is no trailing slash at Subtest\ this would give
// C:\Public\Test instead of C:\Public\Test\Subtest
Console.WriteLine(Path.GetDirectoryName(combined));

Create Random Files

Console.WriteLine(Path.ChangeExtension(Path.GetRandomFileName(), ".txt"));