Skip to content
David Cumps

Path Class

Part of my 2008 study notes for exam 70-536 (.NET Framework 2.0).

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"));

You May Also Like

ArrayList Class

Resizeable, unordered, index-based collection. Suffers from boxing/unboxing when working with value types. Implements IEnumerator, allowing foreach…

BitArray Class

Contains an array of true/false values. Resizable through the Length property. Supports bitwise operations.