← Back to C# Course | Chapter 14: File I/O & Streams | Lesson 4 of 6

Path

In this page:

  1. Path

Path

The Path class manipulates file path strings without touching the filesystem — combining segments, extracting file names, and getting extensions. Path.Combine builds a correct path for the current operating system, avoiding manual slash handling. Using Path methods instead of string concatenation avoids cross-platform path bugs.

Note: Always use Path.Combine instead of manually concatenating folder names with / or \\.

Example: Path

markup
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string fullPath = Path.Combine("data", "reports", "summary.txt");

        Console.WriteLine(fullPath);
        Console.WriteLine(Path.GetFileName(fullPath));
        Console.WriteLine(Path.GetExtension(fullPath));
        Console.WriteLine(Path.GetFileNameWithoutExtension(fullPath));
    }
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.